Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass strongly typed results back to ActionResult?
    text
    copied!<p>I have a view that is strongly typed:</p> <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;MPKwithMVC.Models.SmartFormViewModel&gt;" %&gt; </code></pre> <p>Works great to generate the view, but when I post, I have an ActionResult defined:</p> <pre><code>[AcceptVerbs(HttpVerbs.Post)] public ActionResult Next(MPKwithMVC.Models.SmartFormViewModel model) { .. } </code></pre> <p>Which I would imagine get hit when my next button is clicked (it works if I change the argument to a FormsCollection). I instead get a message saying "No parameterless constructor defined for this object".</p> <p>What am I doing wrong?</p> <p>My SmartFormsViewModel is:</p> <pre><code> [Serializable] public class SmartFormViewModel { public List&lt;Question&gt; Questions { get; set; } public List&lt;Answer&gt; Answers { get; set; } public SmartFormViewModel(List&lt;Question&gt; questions, List&lt;Answer&gt; answers) { this.Questions = questions; this.Answers = answers; } public SmartFormViewModel() { } } </code></pre> <p>And here is the View:</p> <pre><code> &lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;MPKwithMVC.Models.SmartFormViewModel&gt;" %&gt; &lt;%@ Import Namespace="MPKwithMVC.Models" %&gt; &lt;asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"&gt; SmartForms &lt;/asp:Content&gt; &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt; &lt;h2&gt; Questionaire&lt;/h2&gt; &lt;% using (Html.BeginForm("Next", "SmartForms")) { %&gt; &lt;div style="float: left; margin-right: 2em;"&gt; &lt;% Html.RenderPartial("NavigationPanel", Model); %&gt; &lt;/div&gt; &lt;div&gt; &lt;table&gt; &lt;% foreach (Question question in (Model.Questions)) { %&gt; &lt;tr&gt; &lt;td&gt; &lt;div style="text-align: right; width: 20em;"&gt; &lt;%= Html.Encode(question.QuestionText)%&gt; &lt;/div&gt; &lt;/td&gt; &lt;td&gt; &lt;div style="float: left;"&gt; &lt;% if (question.QuestionType == 1) { %&gt; &lt;%= Html.TextBoxFor(model =&gt; model.Answers[(int)question.QuestionID - 1].AnswerValue) %&gt; &lt;% } %&gt; &lt;% if (question.QuestionType == 2) { %&gt; &lt;%= Html.RadioButton("yn" + question.QuestionID, "Yes", false)%&gt;Yes &lt;%= Html.RadioButton("yn" + question.QuestionID, "No", true)%&gt;No &lt;% } %&gt; &lt;/div&gt; &lt;% if (question.Required == true) { %&gt; &lt;div style="color: Red; float: right; margin-left: 3px;"&gt; *&lt;/div&gt; &lt;% } %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;% } %&gt; &lt;tr&gt; &lt;td&gt; &lt;% if (ViewData["errorMsg"] != null) {%&gt; &lt;div style="color:Red;"&gt; &lt;%= Html.Encode(ViewData["errorMsg"].ToString()) %&gt; &lt;/div&gt; &lt;% } %&gt; &lt;/td&gt; &lt;td&gt; &lt;div style="margin-top: 1em;"&gt; &lt;button name="button" value="next"&gt;Next&lt;/button&gt; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;% } %&gt; &lt;/asp:Content&gt; </code></pre>
 

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