Reputation: 11127
So here's a view file from an MSDN tutorial on asp.net:
@model EFDBfirst.Models.Student
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Student</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.LastName)
</dt>
<dd>
@Html.DisplayFor(model => model.LastName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.FirstName)
</dt>
<dd>
@Html.DisplayFor(model => model.FirstName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.EnrollmentDate)
</dt>
<dd>
@Html.DisplayFor(model => model.EnrollmentDate)
</dd>
<dt>
@Html.DisplayNameFor(model => model.MiddleName)
</dt>
<dd>
@Html.DisplayFor(model => model.MiddleName)
</dd>
</dl>
<table class="table">
<tr>
<th>
Course Title
</th>
<th>
Grade
</th>
<th>
Credits
</th>
</tr>
@foreach (var item in Model.Enrollments)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Course.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Grade)
</td>
<td>
@Html.DisplayFor(modelItem => item.Course.Credits)
</td>
</tr>
}
</table>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.StudentID }) |
@Html.ActionLink("Back to List", "Index")
</p>
On the first line there is model
and Models
. I'm pretty sure that Models
refers to the "Models" folder in the project root.
A few lines down there's @Html.DisplayNameFor(model => model.LastName)
which has the model
inside. how is this one different than model
in the first line?
Then again there's @foreach (var item in Model.Enrollments)
which includes Model
now this one doesn't end with an "s", so I don't think it's referring to Models
folder in project root. then what is this?
There is also modelItem
, I'm not sure if it's just a parameter or is referring to something in this class.
And last one is @Html.ActionLink("Edit", "Edit", new { id = Model.StudentID })
, it's similar to the one used in foreach, is this referring to the same thing?
Upvotes: 3
Views: 645
Reputation: 6963
On the first line there is model and Models. I'm pretty sure that Models refers to the "Models" folder in the project root.
Similar to the include statement or using statement you are declaring that this page uses the Student Model which contains all the fields and possibly other stuff you need. It's a binding thing... the page is bound to the model of which when displayed contains an instance of Student.
A few lines down there's @Html.DisplayNameFor(model => model.LastName) which has the model inside. how is this one different than model in the first line?
Remember all rendering is done server side, so this statement is saying to Razor, render me a DisplayName (element type), For the property named LastName found in the model instance of that student being rendered. The first word model could easily be 'p' for parameter if it makes more sense. The first word can be misleading but it's how MVC templates do it.
Then again there's @foreach (var item in Model.Enrollments) which includes Model now this one doesn't end with an "s", so I don't think it's referring to Models folder in project root. then what is this?
Anytime you see a @foreach statement this is telling the server side rendering of this control to iterate through something. In this case it would be the student object Enrollments list. For each enrollment in this student...
There is also modelItem, I'm not sure if it's just a parameter or is referring to something in this class.
Don't try to take the word modelItem literally as you could easily rename it to 'p' for parameter. It's more a question of how do Lambda Expressions work? In this case DisplayFor needs a string to be set to know what to display, modelItem becomes that string value because it 'goes to' item.fieldname to get it. What you don't see is an explicit return statement like "return item.fieldname" because it's implicitly understood when using Lambdas.
And last one is @Html.ActionLink("Edit", "Edit", new { id = Model.StudentID }) , it's similar to the one used in foreach, is this referring to the same thing?
The MODEL is of type student which has a field of StudentID this is how you retrieve the StudentID from the object (Model) of student which is "Bound" to this page. The DataContext of the page is a student instance with an alias of MODEL, the MODEL has a field StudentID which is posted back to the server side when clicked.
Upvotes: 2
Reputation: 53958
This @model EFDBfirst.Models.Student
declares that the model that is used in this view is the Student
.
Inside you view you can refer to your model, using the Model
. So if you want to iterate though the Enrollments
of the student object you have passed to this View, you just have to do this:
@foreach(var enrollment in Model.Enrollments)
{
}
Regarding this, @Html.DisplayNameFor(model => model.LastName)
this is an HTML helper, that will create for you a label in this case for the property of you model that is called LastName
.
Regarding html helpers, please have a look here. Furthermore, you could pass to this helper this line:
@Html.DisplayNameFor(m => m.LastName)
or this line:
@Html.DisplayNameFor(x => x.LastName)
It doesn't matter that you made use of model
. This is just a reference to the model you have passed to the View.
Last but not least, this @Html.ActionLink("Edit", "Edit", new { id = Model.StudentID })
is also an html helper that creates an html link.
Upvotes: 6
Reputation: 4471
@model EFDBfirst.Models.Student
This line is defining what model is going to used for this strongly typed view. @model
is the razor directive that allows you to specify your model. EFDBfirst.Models.Student
is the fully qualified type of your model. By default, this relates to your folder structure but it doesn't have to. If you open your Student
class, you will see the namespace. The namespace + class is what's in the first line.
Each strongly-typed view has a Model
object/property associated with it. This is the object you pass in via your action method. return View(someObject)
. The Model
you see in the foreach loop is the view's model object. Model.StudentId
is also referring to the same object.
The lowercase model
you are seeing is just a name of the parameter in the lambda expression. This is the same as modelItem
that you see. HTML helpers will take your view's Model and provide it to the expression. Since it's an expression, you can name this parameter anything you want. Html.DisplayFor(foo => foo.FirstName)
.
Upvotes: 3