4

I want to pass a list array from the View to the controller on submission of the form. I can pass back simple values by using the Html.hidden() function. But how does one pass back a complex object or a List array

2 Answers 2

0

You can either use Json or look into the following example

http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

Sign up to request clarification or add additional context in comments.

Comments

0

You can pass back a list within a view model using Html.hidden for each element of a list.

The list property in your view model will be re-constructed as long as you process the list elements using a for loop in your view (foreach will not work). For example:

@for (var i = 0; i < Model.Nutrients.Count(); i++) 
{
  // This ensures that the list of nutrients is passed in the view model back to the controller
  @Html.HiddenFor(m => m.Nutrients[i].Name);
  @Html.HiddenFor(m => m.Nutrients[i].Id);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.