Thursday, July 18, 2013

How to bind the records to a list in MVC


  • The major advantage in MVC is there is no need to write Bulk amount of code. Here by writing the single line of code we can bind the records from database to list....

VIEW::viewlist.aspx:
<body>
    <table>
        <tr>
            <th></th>
            <%--<th>
                EmpID
            </th>--%>
            <th>
                Code
            </th>
            <th>
                Salary
            </th>
            <th>
                CustomerName
            </th>
        </tr>

    <% foreach (var item in Model) { %>
    
        <tr>
            <td>
                <%: Html.ActionLink("Edit", "Editcustomer","Customer", new { id =item.EmpID }, new object { })%> |
                <%: Html.ActionLink("Details", "Details", new { id = item.EmpID })%> |
                <%: Html.ActionLink("Delete", "DeleteCustomer", new { id = item.EmpID })%>                
            </td>
           <%-- <td>
                <%: item.EmpID %>
            </td>--%>
            <td>
                <%: item.Code %>
            </td>
            <td>
                <%: item.Salary %>
            </td>
            <td>
                <%: item.CustomerName %>
            </td>
        </tr>
    
    <% } %>

    </table>

    <p>
        <%: Html.ActionLink("Create New", "returntofillcustomer1", "Customer")%>
    </p>

</body>

Controller:: Customer

//Here we are binding the list of customers to list
public ActionResult Viewlist()
        {  
            //here we are returning the list of customers from the model class(from tb_EmpDetails)
            return View(Customer.GetCustomerDetails());
        }

Modelclass:: Customer.cs

//Here we are returning the employee list
 public static List<tb_EmpDetails> GetCustomerDetails( )
        {
            var customer1 = new CustomerEntities1( );//CustomerEntities1 is nothing but Database (.emdf) file
            //adding all the records from tb_EmpDetails to the list and returning the list
            return customer1.tb_EmpDetails.ToList( );
        }







No comments:

Post a Comment