Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET MVC3 Html.HiddenFor inside of foreach loop expecting a collection?

Here is a portion of a partial:

@model IEnumerable<BLL.DomainModel.Jerk>

@foreach (var jerk in Model)
{
    using (Html.BeginForm("AddJerk", "Jerk", FormMethod.Post, new { @class = "jerkListForm" }))
    {
     @Html.HiddenFor(jerk => )
       @jerk.Name
       ...  
    }
}

The type that the HiddenFor lambda is looking for is the same as the @model (IEnumerable), whereas I'm looking for a single object within that IEnumerable.

What am I missing? Why is it still looking for a collection inside of the foreach loop?

like image 483
Withnail Avatar asked Nov 28 '22 03:11

Withnail


1 Answers

@model IEnumerable<Type>

@foreach(var item in Model)
{

    @Html.HiddenFor(model => item)

}

Don't forget that Type must be de/serializable in order for this to work.

like image 53
mega-squall Avatar answered Dec 10 '22 23:12

mega-squall