Hi All,
Just starting out with ASP.NET MVC and have come across a stumbling block already.
The situation is that I have a custom ViewModel to pass to the view, which contains a list of items to be rated ( will be using the jQuery star rating ), so these are created using the radio button helper, to have the same name, just different value, and these render no problem.
I have absolutely no idea how to actually get that back into the post version of my action, however. I just get a 'no parameterless constructor' error. I don't want to use the forms collection - I want my data to remain class based.
Has anybody had to do anything similar?
Many thanks for any advice.
=======================================================================
UPDATE ( basic code included ):
In the HomeController:
public class MyViewModel
{
public MyViewModel(List<Thing> things ) // Thing.cs contains properties name and rating
{
this.Things = things;
}
public List<Thing> Things { get; private set; }
}
public ActionResult Index()
{
List<Thing> things = new List<Thing>();
Thing t;
t = new Thing();
t.name = "One";
t.rating = 1;
things.Add(t);
t = new Thing();
t.name = "Two";
t.rating = 2;
things.Add(t);
return View(new MyViewModel(things));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index( MyViewModel vm)
{
return View();
}
and in the Index page ( Inherits="System.Web.Mvc.ViewPage<MyProject.Controllers.MyViewModel>" )
<% using (Html.BeginForm())
{%>
<ul>
<% for( int t = 0; t<Model.Things.Count; t++)
{%>
<li>
<% for (int i = 1; i < 6; i++)
{
MyProject.Thing thing = Model.Things[i];
%>
<%=Html.RadioButton(String.Format("Things[{0}]", t), i)%>
<% } %>
</li>
<% }%>
</ul>
<input type="submit" value="submit me" />
<% } %>