Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI everyone
i am new to mvc i am facing a problem, i have a simple view that contains a form and a foreach loop the syntax of the loop goes fine if i did this
@model IEnumerable<SearchBox.Models.Students>

but the syntax of labelfor give me error and if i did this
@model SearchBox.Models.Students
to my view then the foreach loop Model is underlined with a red line

What i will hhave to do to resolve my problem kindly help me to get out of it thankx in advance

What I have tried:

Here is View

@model IEnumerable<SearchBox.Models.Students>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>



@using (Html.BeginForm())
{
    <div class="form-horizontal">
        <div class="form-group">
            @Html.LabelFor(model => model.Name, "Name", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-8">
                @Html.EditorFor(model => model.Name, new { htmlattributes = new { @class = "form-control" } })
            </div>
        </div>
    </div>
}



        <p>
            @Html.ActionLink("Create New", "Create")
        </p>
        <table class="table">
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Name)
                </th>
                <th></th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Name)
                    </td>
                    <td>
                        @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                        @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                        @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                    </td>
                </tr>
            }

        </table>


Controller

public class DefaultController : Controller
    {
        private static List<Students> students = new List<Students>()
        {
            new Students() {Id = 1, Name = "Student - 1"},
            new Students() {Id = 2, Name = "Student - 2"},
            new Students() {Id = 3, Name = "Student - 3"},
            new Students() {Id = 4, Name = "Student - 4"},
            new Students() {Id = 5, Name = "Student - 5"},
            new Students() {Id = 6, Name = "Student - 6"},
            new Students() {Id = 7, Name = "Student - 7"},
            new Students() {Id = 8, Name = "Student - 8"},
            new Students() {Id = 9, Name = "Student - 9"}
        };


        // GET: Default
        public ActionResult Index()
        {
            return View(students);
        }
    }
Posted
Updated 13-Apr-17 22:30pm

The declaration
@model IEnumerable<SearchBox.Models.Students>

tells the view, that the provided data is a kind of collection of type Students - this can be used in a loop.

The declaration
@model SearchBox.Models.Students

tells the view that there is only a single object of type students (instead of a collection) and therefor there is noting to loop through.
 
Share this answer
 
Comments
Muhammd Aamir 14-Apr-17 3:45am    
but how i can use both in my view.... my problem still same
Quote:
but how i can use both in my view.


it shall be done in this way if you need to show from only one model
<div class="form-group">
           @Html.LabelFor(model => model.First().Name, "Name", htmlAttributes: new { @class = "control-label col-md-2" })
           <div class="col-md-8">
               @Html.EditorFor(model => model.First().Name, new { htmlattributes = new { @class = "form-control" } })
           </div>
       </div>


else use this Multiple Models in Single View in MVC[^]
 
Share this answer
 
Comments
Muhammd Aamir 14-Apr-17 6:26am    
Great Thankx KB :) it works now
Karthik_Mahalingam 14-Apr-17 6:52am    
welcome:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900