views:

573

answers:

3

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" />
<% } %>
A: 

When your form is posted, only the selected radio button value will be posted back to the server.

You should be able to use the UpdateModel() method to automatically update your model class with the values posted back from the form.

womp
I can't even get to that point though.In the post's action, I'm passing in an instance of the class I gave the view, but it has the runtime error above when the form is submitted.
Paul
Could you post your code? This is likely a slightly unrelated issue..
womp
+2  A: 

Try using a parameterless constructor for the ViewModel. Also, the names of the properties need to match up with the names/ IDs of the controls.

You may need to simplify a little, or even write you're own model binder.

An explanation of model binders and their use here.

A good article on writing a model binder here.

I think you'll need to write your own binder because you're trying to build an array of a complex type. The complex type on it's own would be fine, it's where it's in the array that the problems begin.

Good luck!

Kieron
Ahh, the sweet, sweet smell of success...I didn't need to do anything complicated like use a custom model binder, I simply had the wrong property names in the index rabiobutton loop.Knew it couldn't be that difficult...Now I just need to figure out how to have a default radiobutton so I don't need to validate.Cheers!
Paul
np, good luck (:
Kieron
A: 

if think this might help you http://stackoverflow.com/questions/1361092/asp-net-mvc-viewmodel-pattern

Omu