In this post I will show you how to use web grid in Asp.Net MVC 3 with Razor Engine with customization it contains Edit ,Delete facilities as we find in normal Asp.Net Web Application :
@model IEnumerable<ContactMangerModel.ContactModel>
@{
@{
var grid = new WebGrid(source: Model,
defaultSort: "First Name",
rowsPerPage:5, fieldNamePrefix:"wg_",
canPage:true,canSort:true,
pageFieldName:"pg",sortFieldName:"srt"
);
}
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
@grid.GetHtml(tableStyle:"listing-border",headerStyle:"gridhead",footerStyle:"paging",rowStyle:"td-dark",alternatingRowStyle:"td-light",
columns:
grid.Columns(
grid.Column(header:"", format: @<text><input id="chk" type="checkbox" value="@item.ID" /></text>),
grid.Column("FirstName", "First Name", style: "colFirstName"),
grid.Column("LastName", "Last Name", style: "colLastName"),
grid.Column("Phone", "Phone", style: "colPhone"),
grid.Column("Email", "Email", style: "colEmail"),
grid.Column("ContactType_Name", "Contact Type", style: "colContactType"),
grid.Column("IsActive", "Status",
format: (item) => (item.IsActive) ? Html.Raw("<img src='../../Content/images/active.png' />") :
Html.Raw("<img src='../../Content/images/inactive.png' />"),
style: "colOperation"),
grid.Column(header: "Edit", format: @<text><a href="@Url.Action("Edit", "Contact", new { id = item.id })" ><img src="../../Content/images/edit.png" alt="" style="border:none;" /></a></text>, style: "colOperation"),
grid.Column(header: "Delete", format: @<text><a href="@Url.Action("Delete", "Contact", new { id = item.id })" onclick="javascript:return ConfirmDelete();"><img src="../../Content/images/delete.png" alt="" style="border:none;" /></a></text>, style: "colOperation")
),mode:WebGridPagerModes.Numeric)
</td>
</tr>
</table>
<script type="text/javascript">
function ConfirmDelete() {
return confirm("Are you sure you want to delete contacts?");
}
</script>
My Style Sheet :
.listing-border { background: #fd7d0f;}
.gridhead { background:#FFAA5F; font: bold 13px Arial, Helvetica, sans-serif; color:#000000; text-decoration: none; height: 27px; text-align: left;}
.gridhead th a {text-decoration:none;color:#000000;}
.gridhead th a:hover {text-decoration:underline;color:#FF0000;}
.td-dark { background: #ffffff; height: 20px; }
.td-light { background: #FFE2BF; height: 20px; }
.paging { background: #fd7d0f;text-align: right;color:#000000;}
.paging span { font: bold 12px Arial, Helvetica, sans-serif; color:#FFFFFF; margin-right: 3px; padding: 1px 3px 1px 3px }
.paging a { font: bold 12px Arial, Helvetica, sans-serif; color:#000000; text-decoration: none; margin-right: 3px; border: 1px solid #ffffff; background: #fd7d0f; padding: 1px 3px 1px 3px }
.paging a:hover { font: bold 12px Arial, Helvetica, sans-serif; color:#000000; text-decoration: none; border: 1px solid #ffffff; background: #ffffff; }
.colFirstName{width:18%;text-align:left;}
.colLastName{width:18%;text-align:left;}
.colPhone{width:14%;text-align:left;}
.colEmail{width:19%;text-align:left;}
.colContactType{width:18%;text-align:left;}
.colOperation{width:50px;text-align:center;}
Final Output :
Download Link : Click Here
@model IEnumerable<ContactMangerModel.ContactModel>
@{
ViewBag.Title = "List";
}@{
var grid = new WebGrid(source: Model,
defaultSort: "First Name",
rowsPerPage:5, fieldNamePrefix:"wg_",
canPage:true,canSort:true,
pageFieldName:"pg",sortFieldName:"srt"
);
}
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
@grid.GetHtml(tableStyle:"listing-border",headerStyle:"gridhead",footerStyle:"paging",rowStyle:"td-dark",alternatingRowStyle:"td-light",
columns:
grid.Columns(
grid.Column(header:"", format: @<text><input id="chk" type="checkbox" value="@item.ID" /></text>),
grid.Column("FirstName", "First Name", style: "colFirstName"),
grid.Column("LastName", "Last Name", style: "colLastName"),
grid.Column("Phone", "Phone", style: "colPhone"),
grid.Column("Email", "Email", style: "colEmail"),
grid.Column("ContactType_Name", "Contact Type", style: "colContactType"),
grid.Column("IsActive", "Status",
format: (item) => (item.IsActive) ? Html.Raw("<img src='../../Content/images/active.png' />") :
Html.Raw("<img src='../../Content/images/inactive.png' />"),
style: "colOperation"),
grid.Column(header: "Edit", format: @<text><a href="@Url.Action("Edit", "Contact", new { id = item.id })" ><img src="../../Content/images/edit.png" alt="" style="border:none;" /></a></text>, style: "colOperation"),
grid.Column(header: "Delete", format: @<text><a href="@Url.Action("Delete", "Contact", new { id = item.id })" onclick="javascript:return ConfirmDelete();"><img src="../../Content/images/delete.png" alt="" style="border:none;" /></a></text>, style: "colOperation")
),mode:WebGridPagerModes.Numeric)
</td>
</tr>
</table>
<script type="text/javascript">
function ConfirmDelete() {
return confirm("Are you sure you want to delete contacts?");
}
</script>
My Style Sheet :
.listing-border { background: #fd7d0f;}
.gridhead { background:#FFAA5F; font: bold 13px Arial, Helvetica, sans-serif; color:#000000; text-decoration: none; height: 27px; text-align: left;}
.gridhead th a {text-decoration:none;color:#000000;}
.gridhead th a:hover {text-decoration:underline;color:#FF0000;}
.td-dark { background: #ffffff; height: 20px; }
.td-light { background: #FFE2BF; height: 20px; }
.paging { background: #fd7d0f;text-align: right;color:#000000;}
.paging span { font: bold 12px Arial, Helvetica, sans-serif; color:#FFFFFF; margin-right: 3px; padding: 1px 3px 1px 3px }
.paging a { font: bold 12px Arial, Helvetica, sans-serif; color:#000000; text-decoration: none; margin-right: 3px; border: 1px solid #ffffff; background: #fd7d0f; padding: 1px 3px 1px 3px }
.paging a:hover { font: bold 12px Arial, Helvetica, sans-serif; color:#000000; text-decoration: none; border: 1px solid #ffffff; background: #ffffff; }
.colFirstName{width:18%;text-align:left;}
.colLastName{width:18%;text-align:left;}
.colPhone{width:14%;text-align:left;}
.colEmail{width:19%;text-align:left;}
.colContactType{width:18%;text-align:left;}
.colOperation{width:50px;text-align:center;}
Final Output :
Download Link : Click Here
This is a nice article..
ReplyDeleteIts easy to understand ..
And this article is using to learn something about it..
c#, dot.net, php tutorial, Ms sql server
Thanks a lot..!
ri70
This is great. I was pulling my hair out searching for an edit/delete button for Razor web grid.
ReplyDeleteA lot of articles, but this was the only one that works right out of the gate.
Thanks.
Hi..
ReplyDeleteThanks a lot for your warm comments..
If you still need any help you can post your questions here..
Thanks,
Jain Bipin
Thanks, finally i found exactly that i need. Thanks again.
ReplyDeletehello. I wonder if we could add a header to the checkbox column that will trigger all the checkboxes like select all? Thanks for the article, it is a great help!
ReplyDeleteHi Badz,
ReplyDeleteThanks for your appreciation..
As you told about CheckBox in header of webgrid, sadly I have to say that before Post this artical I have tried it so much but didn't got any success...
The reason behind it is ,the header property of webgrid only takes "string" argument so even if we forcefully set "checkbox" there then it will show it as a html text...
But if you got any solution for this then please tell me also....
Sure no problem. Right now I am trying to figure it out too. Thanks for your quick response. Hope that we get the solution :)
ReplyDeleteHope So...
ReplyDeleteThanks..
Good article.Post some Flashy Menus in MVC 3 using Jquery
ReplyDeleteThanks..
ReplyDeleteWhat type of menu you want?....If you have any demo or example then show me so that I can do it....
in asp.net, I read image files in a directory to datalist and display them with rows and columns and paging. How to do that in mvc razor?
ReplyDeleteThanks,
kev
Can you share the solution and then we can have the project including the images?
ReplyDeleteThanks.
Please give your email.
DeleteHi I found this article invaluable in getting me started on formatting the grid. One of the main disappointments of the grid is not being able to style the footer (built in ajax paging/sorting would have been great too!).
ReplyDeleteSo I extended your example by adding the following css to :
.webgrid-footer
{
padding: 6px 5px;
background-color: #eee;
border-top: 2px solid #3966A2;
height: 30px;
border-bottom: 2px solid #D6E8FF;
border-left: 2px solid #D6E8FF;
border-right: 2px solid #D6E8FF;
display: block;
color: #111;
line-height: 30px;
font-family: Arial, Sans-Serif;
font-size: 13px;
}
.webgrid-footer td
{
float: right; /* if you want the footer right aligned else use 'left'
}
.webgrid-footer a
{
border: 1px solid #aaa;
border-top-left-radius: 2px;
border-top-right-radius: 2px;
border-bottom-right-radius: 2px;
border-bottom-left-radius: 2px;
color: #111;
border-color: #aaa;
box-shadow: inset 0px 1px 0px #fff, 0px 1px 3px rgba(0,0,0,0.15);
background-color: rgb(238, 238, 238);
margin-right: 3px;
padding: 4px 9px 4px 9px;
}
.webgrid-footer a:hover
{
border: 1px solid #888;
}
hopefully someone will find this a useful starting point
I am not agree with you, because I have tested it and other users have also used it and none have problem.
DeleteSo, there may be some issue with your code.
Please check and review your source code.
Hi,After listing to all I am planing to give full source code of application, so you can view running application and have better idea. Just Send me your Email-ID and I will forward my source code on mail.
ReplyDeleteHi Thanks a lot , can you send me your full source code, so that i will get better idea about WebGrid, so can i expect the mail by Today or Tomorrow.
DeleteMy mail is as Below.
rkpotu@gmail.com
Thanks
Ramu
Can you kindly share your working code to avi.chars@gmail.com. Please.. It is very urgent.. :(
ReplyDeleteIs it possible to put dropdown filter option with each header for easy filter.
ReplyDeleteYes it is possible but you need to dig in to it deeply.
DeleteHi Thanks a lot, i learned a lot with this article, Cou8ld you please post Complete solution,Full controlls and Models and DB CODE as well. otherwise possible pls post complete solution.
ReplyDeletePlease share your email so I can share complete solution.
DeleteCan you please share the full source code to sharmi.santha@gmail.com
ReplyDeleteCan you please share the full source code to vanamsrikanth@gmail.com
ReplyDeletecan you tell me what is purpose of checkbox here. if i am not able to select multiple check box and on one click i deleted all the selected rows then what use of check box.
ReplyDeleteThanks a lot ... its a very helpful article
ReplyDeleteHi Biswaranjan,
ReplyDeletePurpose of checkbox here it to do multiple operations at a time on selected items such as update, delete etc.
For selecting all checkbox at time then you need to find way on your own.
This article is prepared to give basic idea of webgrid.
Please read all comments and you will get your answer. Read following comment which I have replied earlier :
"As you told about CheckBox in header of webgrid, sadly I have to say that before Post this artical I have tried it so much but didn't got any success...
The reason behind it is ,the header property of webgrid only takes "string" argument so even if we forcefully set "checkbox" there then it will show it as a html text... "
Thanks,
Bipin Jain
Could you please send me the complete solution with all the source code? Thank you.
ReplyDeletePlease send your Email Id for that.
DeletePls can u send me the entire code of the above article on my eail id dhirajb4u1981@gmail.com
DeleteHi Jain can you please inbox me the entire code, I'm new to webgrid and MVC. Email is: joseph.kayima@gmail.com
DeleteCould you please send me the complete solution with all the source code? Thank you.
ReplyDeleteMy Email : vinalysilva@gmail.com
Thanks
ReplyDeleteNice article...
Greate..
ReplyDeleteAwesome one... its easy to understand pls share the solution @ vmanjunnaath@gmail.com
ReplyDeleteThanks for nice article, Could you please send me the complete solution with all the source code? Thank you.
ReplyDeleteMy Email : luvyjoshi@gmail.com
Hi,
ReplyDeleteExcellent article... Thanks for your time on sharing your expertise.
can you please share the complete solution file with me to email id cgrgsm@gmail.com
thanks,
GSM
Nice one really..
ReplyDeleteCan you share the solution @ vishnutejcj@gmail.com
Nice article. Would you share the solution @ lifesharing88@gmail.com. Thanks
ReplyDeleteThank you so much to all.
ReplyDeletePlease use this link to download source : http://sdrv.ms/10XGHEw
It's good one but while we are run this downloadable application.It should be runnable project.But this project got issues.But coding is fine for understanding the concept..you can reach me pardhasaradhi06@gmail.com
ReplyDeletechek this link for another good example
ReplyDeletehttp://articlesforprogramming.blogspot.in/2013/07/webgrid-in-aspnet-mvc.html
some help with source code of the whole app: E-mail:joseph.kayima@gmail.com
ReplyDeleteI spent 2hrs to run the code of your application. Finally setup database. Now it requires the stored procedure also. So, please provide a link of database. OR even better simply use the database in App_Data folder. BTW Nice code.
ReplyDeleteLooking forward from you.
Hello guys,
ReplyDeleteThank you so much for using my blog and supprt. I have already attached both backup file as well script also. So, for those .bak file won't work can use DB.sql file.
If you want to use DB.sql then first create Database named "ContactManager" in your SQL Server Management Studio and then run my attached script.
This code is based on Entity Framework, so no Stored Procedure is used in whole application and none is created in Database.
Please do not forget to update connection string in web.config file.
Please post your any question here and I will reply as soon as possible. I have lots of rush over my email so I won't be able to reply on email.
Thanks,
Bipin Jain
When I am trying to open this project i have find below error wile i have installed MVC 4 and I am working in mvc 4 application , below is error
ReplyDeleteLoading E:\MVC Applications\SampleRazor\SampleRazor\SampleRazor\SampleRazor.csproj ...
E:\MVC Applications\SampleRazor\SampleRazor\SampleRazor\SampleRazor.csproj : error : The project file 'E:\MVC Applications\SampleRazor\SampleRazor\SampleRazor\SampleRazor.csproj' cannot be opened.
The project type is not supported by this installation.
Loading E:\MVC Applications\SampleRazor\SampleRazor\SampleRazor\SampleRazor.csproj ...
E:\MVC Applications\SampleRazor\SampleRazor\SampleRazor\SampleRazor.csproj : error : The project file 'E:\MVC Applications\SampleRazor\SampleRazor\SampleRazor\SampleRazor.csproj' cannot be opened.
The project type is not supported by this installation.
Hi! Great to have such an informative post like this. Thank u for including the useful information in the article. This a good blog on project management. Here i found another site which contains more information on project management.
ReplyDeleteonline project management
Please send me the solution for insert,update checkbox value in webgrid in mvc 4 and save checkbox value to database
ReplyDeletePlease send me the solution for insert,update checkbox value in webgrid in mvc 4 and save checkbox value to database
Deletesend me the above application and sourcecode in asp.net mvc on pratikp_278@yahoo.com
Hi,
ReplyDeleteThanks. Please try to download project again, I have made some changes to it so it will run without any issue.
How to transfor data from one web application to another web application
ReplyDeleteYou can do this stuff in 2 ways :
Delete1. Generate Schema and Data script from DB and run it over another application DB.
2. Write custom code to fetch data from 1 DB and Save to another DB.
Hi i am new in mvc.I designed the grid view and data display with out action .
ReplyDeleteif i use this new { id = item.id }
An exception of type 'System.Reflection.AmbiguousMatchException' occurred in mscorlib.dll but was not handled in user code
.Please send the entire code in my email:smrutiranjansahoo55@gmail.com
Download link given in this post itself contains complete source code.
ReplyDelete'System.Reflection.AmbiguousMatchException' stating that you have more than column or property with same name and it is throwing ambiguity exception. Please check your code carefully.
plz send the complete source code to silipkumar.777@gmail.com.....urgent
ReplyDeletehi sir,this article working great.
ReplyDeletei have a doubt,when i click the edit button in webgrid it show id in url like "http://localhost:12500/Product/ProductUpdate/1" how i will catch that id.
Thank you