What is the cleanest solution to post dynamic List<> inside ViewModel.
Let's say you have a
public class UsersVM
{
public string Name {get; set;}
public string Surname {get; set;}
public List<Friends> Friends {get; set;}
}
public class Friends
{
public string Name {get; set;}
public string Surname {get; set;}
}
And in the view i create the table from List<Friends> adding remove and delete buttons in the last column of every row. Delete removes the row with a simple jquery and Add button adds a new Friend as row to the table. For now i have a lot of javascript that
makes a friend model and posts it from table, but there must be a simpler way?
Thanks for your responses. I have already looked at suggested links and implemented a solution that works as described in my question. For someone reading this post i recommend to look at:
theeyeabove
Member
691 Points
177 Posts
Post dynamic ViewModel
Mar 27, 2012 09:24 AM|LINK
What is the cleanest solution to post dynamic List<> inside ViewModel.
Let's say you have a
public class UsersVM { public string Name {get; set;} public string Surname {get; set;} public List<Friends> Friends {get; set;} } public class Friends { public string Name {get; set;} public string Surname {get; set;} }And in the view i create the table from List<Friends> adding remove and delete buttons in the last column of every row. Delete removes the row with a simple jquery and Add button adds a new Friend as row to the table. For now i have a lot of javascript that makes a friend model and posts it from table, but there must be a simpler way?
jack.chen
Member
14 Points
2 Posts
Re: Post dynamic ViewModel
Mar 27, 2012 09:40 AM|LINK
hi,you can organize the table like that:
<table>
<tr><td><input name="[0].Name"/></td><td><input name="[0].Surname"/> </td></tr>
<tr><td><input name="[1].Name"/></td><td><input name="[1].Surname"/> </td></tr>
...........................
<tr><td><input name="[i].Name"/></td><td><input name="[i].Surname"/> </td></tr>
</table>
and accept datas in controller like:
public actionresult save(List<Friends> friends), then it will work,
hope help you !
raduenuca
All-Star
24675 Points
4250 Posts
Re: Post dynamic ViewModel
Mar 27, 2012 09:44 AM|LINK
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Radu Enuca | Blog
theeyeabove
Member
691 Points
177 Posts
Re: Post dynamic ViewModel
Mar 27, 2012 11:16 AM|LINK
Thanks for your responses. I have already looked at suggested links and implemented a solution that works as described in my question. For someone reading this post i recommend to look at:
http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/
I think i'll use knockoutJS, it's much cleaner.
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Post dynamic ViewModel
Mar 27, 2012 03:34 PM|LINK
Here's an example I wrote, for a talk, that shows you how to use KnockoutJS with binding to collections
http://bit.ly/qGzjn1
Blog | Twitter : @Hattan