Blog Archives

Gridview Model binding with Control attribute as parameter

Asp.Net MVC had enjoyed the benefit of Model binding since its inception. It helped in few things such as

  • UI and data model separation.
  • Unit testing of the data binding method. (great! isn’t it?)

With Asp.Net 4.5, Model binding is now available for Web forms as well. Moreover it is integrated in a seamless manner.

so here we go!

UI Code:

New Gridview has two properties to be noticed.

  • ItemType:
  • It needs a type name that you are binding to. It can be a dll which contains the actual method.

  • SelectMethod:
  • It needs a method which returns an IEnumerable or IQueryable type.

    Similarly we have UpdateMethod which is not covered in this post.

    <asp:DropDownList ID="ddlNameFilter" runat="server" AutoPostBack="true" Width="500px" >
                    <asp:ListItem Value="" Text="-- Select word name starts with --" Selected="True" />
                    <asp:ListItem Value="KE" Text="KE" />
                    <asp:ListItem Value="TE" Text="TE" />
             <asp:GridView ID="gvPersons" runat="server" 
                    AllowPaging="True" AutoGenerateColumns="False" PageSize="20"             
                 ItemType="WebFormSamples.Person" SelectMethod="GetPersonList" 
                        <asp:BoundField DataField="BusinessEntityID" HeaderText="Product"
                            SortExpression="BusinessEntityID" />
                        <asp:BoundField DataField="PersonType" HeaderText="PersonType"
                            ReadOnly="True" SortExpression="PersonType" />
                        <asp:BoundField DataField="FirstName" HeaderText="FirstName"
                            SortExpression="FirstName" />
                        <asp:BoundField DataField="LastName" HeaderText="LastName"
                            SortExpression="LastName" />
                    <PagerStyle HorizontalAlign="Right" />
                    <PagerSettings Mode="Numeric" />

    Code Behind:

            // ddlNameFIlter is a dropdown which user selects and selected value is passed as parameter to the below //<strong>SelectMethod</strong> of Gridview. 
            public IQueryable<Person> GetPersonList([Control("ddlNameFilter")] string searchName)
                List<Person> persons = new List<Person>();
                if ( string.IsNullOrEmpty(searchName))
                    persons = Person.GetPersonList();
                    persons = Person.GetPersonList().Where(p => p.FirstName.ToLower().StartsWith(searchName.ToLower())).ToList();
                return persons.AsQueryable();

    There are few more Value providers introduced in Asp.Net 4.5. [QueryString],[Cookie] are also very useful.

    I hope it helps!


    Asp.Net MVC – handling multiple submit from a view

    In Asp.Net MVC, after understanding of how controller communicates with Views, the biggest challenge i found was to handle multiple form submit requests. Almost every view will have multiple buttons to post/submit data (ideally a ViewModel) to controller. In this post I am assuming that you are comfortable with very basics of MVC.

    To do this sample just create a Asp.Net MVC web application project and write the code in “Views/Index.cshtml” at the end because by default this is the landing page of default website template.

    This code is just a sample which may not be practical in real world scenario.

    @using (Html.BeginForm("MyCommonSubmitAction", "Home", FormMethod.Post)) {
    <input type ="text" id="txtComments"  /> <br />
    <input type="submit" name="approve" id="btnApprove" value="Approve"/>
    <input type="submit" name="reject" id="btnReject" value="Reject" />

    Now, Let us see the controller action method how it will differentiate these two submits.

    public ActionResult MyCommonSubmitAction(FormCollection frm)
    var button = frm["approve"] ?? frm["reject"];
    //var comments  =  frm["txtComments"]; assuming view is not strongly typed
    switch (button )
    case "Approve" :
    // do your thing
    case "Reject"  :
    // do your thing
    return View("Index");
    return View("Index");

    Now, this is just one of the ways you can differentiate multiple submit requests. Important thing to notice here is that FormCollection
    is bound to the post request and is available as a parameter which actually holds all the form fields posted to the controller. Ideally every view should be bound to a strongly type ViewModel.

    There are better ways of binding your data model to your view. Those who are new to Asp.Net MVC. I would recommend you to read Scott Gu post on model binding.