I have a model class that has a couple of properties that are lists which are lists of options a user may select.
public class OrderViewModel
{
public string OrderName{get; set;}
public DateTime TimeStamp{get; set;}
public List<UserOption> FirstOptions{get; set;}
public LIst<UserOption> SecondOptions{get; set;}
}
public class UserOption
{
public Guid Id{get; set;}
public bool Selected{get; set;}
public string Name{get; set;
}
My question is, how in the View can I represent this and get the values back when I do my post?
I have tried creating a table for each set of options and then looping through the items in the lists to create table rows. However when I post I never get the items back. I saw a couple of discussions where people used Hidden Elements to get the Id into
the Html and for them it worked but that hasn't worked for me.
the browser only submits input, select and textareas that are children of the form doing the submit. also the elements need to be named properly. you should use array naming:
@for (vari=0; i < Model.FirstOptions.Count; ++i)
{
@HiddenFor(m=>m.FirstOptions[i].Id)
}
bruce (sqlwork.com)
Marked as answer by MacKenzie on Jan 11, 2012 06:31 PM
MacKenzie
Member
39 Points
13 Posts
MVC Model with List Properties
Jan 11, 2012 04:57 PM|LINK
Hello all,
I have a model class that has a couple of properties that are lists which are lists of options a user may select.
public class OrderViewModel { public string OrderName{get; set;} public DateTime TimeStamp{get; set;} public List<UserOption> FirstOptions{get; set;} public LIst<UserOption> SecondOptions{get; set;} } public class UserOption { public Guid Id{get; set;} public bool Selected{get; set;} public string Name{get; set; }My question is, how in the View can I represent this and get the values back when I do my post?
I have tried creating a table for each set of options and then looping through the items in the lists to create table rows. However when I post I never get the items back. I saw a couple of discussions where people used Hidden Elements to get the Id into the Html and for them it worked but that hasn't worked for me.
Any help or links would be greatly appreciates
Thank you in advance
MacK
bruce (sqlwo...
All-Star
37614 Points
5573 Posts
Re: MVC Model with List Properties
Jan 11, 2012 05:04 PM|LINK
the browser only submits input, select and textareas that are children of the form doing the submit. also the elements need to be named properly. you should use array naming:
@for (vari=0; i < Model.FirstOptions.Count; ++i)
{
@HiddenFor(m=>m.FirstOptions[i].Id)
}
ignatandrei
All-Star
137670 Points
22146 Posts
Moderator
MVP
Re: MVC Model with List Properties
Jan 11, 2012 05:13 PM|LINK
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx