Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • foreach in MVC view

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 47
    Comment on it

    In MVC project we need to bind multiple values from the controller action to the view so for doing that predefined templates can be used.

     

     

     

    But in any case you want to use your own code then you need to understand to bind the multiple values that can be a single row or multiple row.

     

     

     

    If the values are of single row type then we can bind it directly and no loop is required for that but if these values are of multiple row type then there is need of performing the looping for binding all values into the view controls.

     

     

     

    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.EmpId)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.FirstName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.MiddleName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.LastName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.PhoneNo)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Address)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.msg)
            </th>
            <th></th>
        </tr>
    
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.EmpId)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.FirstName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.MiddleName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.LastName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.PhoneNo)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Address)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.msg)
                </td>
                <td>
                    <a class="lnkEdit" id='@item.EmpId'>Edit</a> | |
                    @Html.ActionLink("Details", "Details", new { id = item.EmpId }, new { @class = "lnkDetail" }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.EmpId }, new { @class = "lnkDelete" })
                </td>
            </tr>
        }
    
    </table>
    

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: