Tuesday, April 12, 2011

Web Grid with Razor Engine,Paging and Customized Edit and Delete facility

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>

@{
          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


56 comments:

  1. This is a nice article..
    Its 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

    ReplyDelete
  2. This is great. I was pulling my hair out searching for an edit/delete button for Razor web grid.
    A lot of articles, but this was the only one that works right out of the gate.

    Thanks.

    ReplyDelete
  3. Hi..
    Thanks a lot for your warm comments..
    If you still need any help you can post your questions here..

    Thanks,
    Jain Bipin

    ReplyDelete
  4. Thanks, finally i found exactly that i need. Thanks again.

    ReplyDelete
  5. hello. 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!

    ReplyDelete
  6. Hi Badz,
    Thanks 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....

    ReplyDelete
  7. Sure no problem. Right now I am trying to figure it out too. Thanks for your quick response. Hope that we get the solution :)

    ReplyDelete
  8. Good article.Post some Flashy Menus in MVC 3 using Jquery

    ReplyDelete
  9. Thanks..
    What type of menu you want?....If you have any demo or example then show me so that I can do it....

    ReplyDelete
  10. 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?

    Thanks,
    kev

    ReplyDelete
  11. Can you share the solution and then we can have the project including the images?

    Thanks.

    ReplyDelete
  12. Hi 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!).

    So 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

    ReplyDelete
    Replies
    1. I am not agree with you, because I have tested it and other users have also used it and none have problem.

      So, there may be some issue with your code.
      Please check and review your source code.

      Delete
  13. 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.

    ReplyDelete
    Replies
    1. Hi 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.
      My mail is as Below.

      rkpotu@gmail.com

      Thanks
      Ramu

      Delete
  14. Can you kindly share your working code to avi.chars@gmail.com. Please.. It is very urgent.. :(

    ReplyDelete
  15. Is it possible to put dropdown filter option with each header for easy filter.

    ReplyDelete
    Replies
    1. Yes it is possible but you need to dig in to it deeply.

      Delete
  16. Hi 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.

    ReplyDelete
    Replies
    1. Please share your email so I can share complete solution.

      Delete
  17. Can you please share the full source code to sharmi.santha@gmail.com

    ReplyDelete
  18. Can you please share the full source code to vanamsrikanth@gmail.com

    ReplyDelete
  19. can 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.

    ReplyDelete
  20. Thanks a lot ... its a very helpful article

    ReplyDelete
  21. Hi Biswaranjan,
    Purpose 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

    ReplyDelete
  22. Could you please send me the complete solution with all the source code? Thank you.

    ReplyDelete
    Replies
    1. Please send your Email Id for that.

      Delete
    2. Pls can u send me the entire code of the above article on my eail id dhirajb4u1981@gmail.com

      Delete
    3. Hi Jain can you please inbox me the entire code, I'm new to webgrid and MVC. Email is: joseph.kayima@gmail.com

      Delete
  23. Could you please send me the complete solution with all the source code? Thank you.
    My Email : vinalysilva@gmail.com

    ReplyDelete
  24. Awesome one... its easy to understand pls share the solution @ vmanjunnaath@gmail.com

    ReplyDelete
  25. Thanks for nice article, Could you please send me the complete solution with all the source code? Thank you.
    My Email : luvyjoshi@gmail.com

    ReplyDelete
  26. Hi,
    Excellent 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

    ReplyDelete
  27. Nice one really..

    Can you share the solution @ vishnutejcj@gmail.com

    ReplyDelete
  28. Nice article. Would you share the solution @ lifesharing88@gmail.com. Thanks

    ReplyDelete
  29. Thank you so much to all.

    Please use this link to download source : http://sdrv.ms/10XGHEw

    ReplyDelete
  30. 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

    ReplyDelete
  31. chek this link for another good example
    http://articlesforprogramming.blogspot.in/2013/07/webgrid-in-aspnet-mvc.html

    ReplyDelete
  32. some help with source code of the whole app: E-mail:joseph.kayima@gmail.com

    ReplyDelete
  33. I 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.

    Looking forward from you.

    ReplyDelete
  34. Hello guys,

    Thank 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

    ReplyDelete
  35. 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


    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.


    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.

    ReplyDelete
  36. 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.
    online project management

    ReplyDelete
  37. Please send me the solution for insert,update checkbox value in webgrid in mvc 4 and save checkbox value to database

    ReplyDelete
    Replies
    1. Please send me the solution for insert,update checkbox value in webgrid in mvc 4 and save checkbox value to database


      send me the above application and sourcecode in asp.net mvc on pratikp_278@yahoo.com

      Delete
  38. Hi,

    Thanks. Please try to download project again, I have made some changes to it so it will run without any issue.

    ReplyDelete
  39. How to transfor data from one web application to another web application

    ReplyDelete
    Replies
    1. You can do this stuff in 2 ways :

      1. 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.

      Delete
  40. Hi i am new in mvc.I designed the grid view and data display with out action .
    if 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

    ReplyDelete
  41. Download link given in this post itself contains complete source code.

    '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.

    ReplyDelete
  42. plz send the complete source code to silipkumar.777@gmail.com.....urgent

    ReplyDelete
  43. hi sir,this article working great.
    i 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

    ReplyDelete