user547794 user547794 -4 years ago 257
C# Question

Creating HiddenFor IEnumerable<String> in View

I have a Property that is an IEnumerable

public IEnumerable<string> ChangesOthersResult { get; set; }

I need to collect all values from ChangesOthersResult and post from a view back to the controller. How can I loop through the Ienumerable and create hidden fields that will bind back to the ViewModel in the controller?

foreach(var item in Model.ChangesOthersResult)
@Html.HiddenFor(x => x.ChangesOthersResult);

Is giving me the Raw SQL statement as text.

Answer Source

Convert ChangesOthersResult to an array and use a for loop to output something like this:

for (int i = 0; i < Model.ChangesOthersResult.Length; i++ )    
   @Html.Hidden("ChangesOthersResult[" + i + "]", ChangesOthersResult[i])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download