I am new to ASP mvc framework and C#. The following code below is auto generated by visual studio. I am very confused with the model object. I think(correct me if i'm wrong) it must be from the control class which implemented the View(object model)
method. Does the '@model IEnumerable' cast the model
object as a enumerator? I know some programming languages which does that. Also, when i rename the model
to something else, i get an error which states context not found and the aModel => aModal.Name
gets an error. Furthermore, the @model IEnumerable<ExploreCalifornia.Models.Tour>
does not look like it is sequential (what i mean is that it does not need to be on top). If i remove the @model IEnumerable<ExploreCalifornia.Models.Tour>
i get errors for the aModal => modal.Name
. However, when i put @model IEnumerable<ExploreCalifornia.Models.Tour>
anywhere (even on the last line), the errors are fixed. I don't even understand why must they use the lambda operator. I feel like c# is questioning my very basic logic of programming, (no need of defining objects, not sequential, etc.)
@model IEnumerable<ExploreCalifornia.Models.Tour> @*Renaming or removing this will cause an error*@
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(aModal => aModal.Name) @*an error is caused when removing the top line code*@
</th>
<th>
@Html.DisplayNameFor(aModal => aModal.Description) @*here too.*@
</th>
</tr>
</table>
@*Putting the top line code here fixes the errors for some reason? Shouldnt it be sequential?*@
Lastly, as you can see, the Html.DisplayNameFor(aModal => aModal.Name)
has an expression using lambda operator. I don't really get why they do it like that. Why cant they directly use the string found in aModal.Name
. If you look at the method signature below, you need at least 2 arguments, but the method implemented has only 1 argument. What is going on here too?
public static MvcHtmlString DisplayNameFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression);
public static MvcHtmlString DisplayNameFor<TModel, TValue>(this HtmlHelper<IEnumerable<TModel>> html, Expression<Func<TModel, TValue>> expression);