How to access Model property(Like @Html.EditorFor(x=>Model.Name)
) in Razor view of IEnumerable Type without using Loops??I.e If a view is strongly typed to some Model holding Model as a LIST.
Eg.
@model IEnumerable<EFTest2.DAL.package_master>
Then is it possible to Display TestBoxFor or EditorFor (to create new Model) Html helper without using foreach Loop.???
Best Answer
When some model property is of type
IEnumerable<SomeType>
you would normally define an editor/display template (~/Views/Shared/EditorTemplates/SomeType.cshtml
or~/Views/Shared/DisplayTemplates/SomeType.cshtml
). This template will be automatically rendered for each element of the collection so that you don't need to write loops:and inside the template you will be able to access individual properties:
Now, if you absolutely need to directly access some element inside a view which is strongly typed to
IEnumerable<SomeType>
you would better use some other collection type such asIList<SomeType>
orSomeType[]
as view model which will give you direct access to elements by index and you will be able to do this for example to access the 6th element of the collection: