Note that there are some explanatory texts on larger screens.

plurals
  1. POasp.net mvc : custom ViewModel form post
    primarykey
    data
    text
    <p>Just starting out with ASP.NET MVC and have come across a stumbling block already.</p> <p>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.</p> <p>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.</p> <p>Has anybody had to do anything similar?</p> <p>Many thanks for any advice.</p> <p>=======================================================================</p> <p>UPDATE ( basic code included ):</p> <pre><code> In the HomeController: public class MyViewModel { public MyViewModel(List&lt;Thing&gt; things ) // Thing.cs contains properties name and rating { this.Things = things; } public List&lt;Thing&gt; Things { get; private set; } } public ActionResult Index() { List&lt;Thing&gt; things = new List&lt;Thing&gt;(); 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&lt;MyProject.Controllers.MyViewModel&gt;" ) &lt;% using (Html.BeginForm()) {%&gt; &lt;ul&gt; &lt;% for( int t = 0; t&lt;Model.Things.Count; t++) {%&gt; &lt;li&gt; &lt;% for (int i = 1; i &lt; 6; i++) { MyProject.Thing thing = Model.Things[i]; %&gt; &lt;%=Html.RadioButton(String.Format("Things[{0}]", t), i)%&gt; &lt;% } %&gt; &lt;/li&gt; &lt;% }%&gt; &lt;/ul&gt; &lt;input type="submit" value="submit me" /&gt; &lt;% } %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload