Note that there are some explanatory texts on larger screens.

plurals
  1. PONet mvc4 form2 being submitted with empty model after form1 posts on the same page successfully returning data to the same model
    text
    copied!<p>I'm using one form with two form posts in it. The first form ("frmTemps") successfully retreieves data and displys it on the form. I can assure you that via debugging the data is in the model when coming back to the View to display the data.</p> <p>Once the data comes back, another button is displayed on the same View, but a second form ("frmProcess") to to some processing on the seclectd data coming back from the first form ("frmTemps"). I know you can have multiple forms on the sme View. It's all part of the control that MVC gives you.</p> <p>The problem is that as soon as I post my second form ("<strong>frmProcess</strong>") <strong>and have a breakpoint set immediately in that action result</strong> and I check the contents of the model, I find out that <strong>all the items in the model are either null, zero, or false.</strong> Via debugging on the model object.</p> <pre><code>[HttpPost] public ActionResult GeneratePDF(ViewModelTemplate_Guarantors model) { </code></pre> <p>I'm using the same model in form2 as I am in form 1.</p> <p>I thought is was understood that after filling out data on a form and after submitting it and having your model type receive it in the action method, this is the standard way to do things.</p> <p>Below, is my ViewModel.</p> <pre><code>public partial class ViewModelTemplate_Guarantors { public int SelectedTemplateId { get; set; } public IEnumerable&lt;PDFTemplate&gt; Templates { get; set; } public int SelectedGuarantorId { get; set; } public IEnumerable&lt;tGuarantor&gt; Guarantors { get; set; } public string LoanId { get; set; } public string SelectedDeptText { get; set; } public string SelectedDeptValue { get; set; } public string LoanType { get; set; } public bool ShowTemps { get; set; } public string Error { get; set; } public string ErrorT { get; set; } public string ErrorG { get; set; } public bool ShowGeneratePDFBtn { get; set; } } </code></pre> <p>Below is my View:</p> <pre><code>@model PDFConverterModel.ViewModels.ViewModelTemplate_Guarantors @{ ViewBag.Title = "BHG :: PDF Generator"; } &lt;h2&gt;@ViewBag.Message&lt;/h2&gt; &lt;div&gt; &lt;table style="width: 1000px"&gt; &lt;tr&gt; &lt;td colspan="5"&gt; &lt;img alt="BHG Logo" src="~/Images/logo.gif" /&gt; &lt;/td&gt; &lt;/tr&gt; @using (Html.BeginForm("Refresh", "Home", FormMethod.Post, new { id = "frmTemps" })) { &lt;tr&gt; &lt;td&gt; @*@(Html.Kendo().NumericTextBox&lt;int&gt;() .Name("txtLoanID") .Placeholder("Enter numeric value") )*@ @Html.LabelFor(model =&gt; model.LoanId) @Html.TextBoxFor(model =&gt; model.LoanId) @Html.ValidationMessageFor(model =&gt; model.LoanId) &lt;td colspan="3"&gt; &lt;input type="submit" id="btnRefresh" value='Refresh' /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;@Html.LabelFor(model =&gt; model.LoanType) @Html.TextBox("SBA", "SBA") @Html.ValidationMessageFor(model =&gt; model.LoanType) @*@Html.TextBoxFor(model =&gt; model.LoanType)*@ &lt;/td&gt; &lt;td&gt; &lt;label for="ddlDept"&gt;Department:&lt;/label&gt; @(Html.Kendo().DropDownListFor(model =&gt; model.SelectedDeptText) .Name("ddlDept") .DataTextField("DepartmentName") .DataValueField("DepartmentID") .Events(e =&gt; e.Change("Refresh")) .DataSource(source =&gt; { source.Read(read =&gt; { read.Action("GetDepartments", "Home"); }); }) ) @Html.ValidationMessageFor(model =&gt; model.SelectedDeptText) &lt;/td&gt; &lt;/tr&gt; } @using (Html.BeginForm("GeneratePDF", "Home", FormMethod.Post, new { id = "frmProcess" })) { if (Model.ShowGeneratePDFBtn == true) { if (Model.ErrorT != string.Empty) { &lt;tr&gt; &lt;td colspan="5"&gt; &lt;u&gt;&lt;b&gt;@Html.Label("Templates:")&lt;/b&gt;&lt;/u&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; @foreach (var item in Model.Templates) { &lt;td&gt; @Html.CheckBoxFor(model =&gt; item.IsChecked) @Html.DisplayFor(model =&gt; item.TemplateName) &lt;/td&gt; } &lt;/tr&gt; } else { Model.Error = Model.ErrorT; } if (Model.ErrorG != string.Empty) { &lt;tr&gt; &lt;td colspan="5"&gt; &lt;u&gt;&lt;b&gt;@Html.Label("Guarantors:")&lt;/b&gt;&lt;/u&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; @foreach (var item in Model.Guarantors) { &lt;td&gt; @Html.CheckBoxFor(model =&gt; item.isChecked) @Html.DisplayFor(model =&gt; item.GuarantorFirstName)&amp;nbsp;@Html.DisplayFor(model =&gt; item.GuarantorLastName) &lt;/td&gt; } &lt;/tr&gt; } else { Model.Error = Model.ErrorG; } &lt;tr&gt; &lt;td&gt; &lt;input type="submit" id="btnGeneratePDF" value='Generate PDF' /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="5"&gt; @Model.Error &lt;/td&gt; &lt;/tr&gt; } } &lt;/table&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; $('btnRefresh').on('click', '#btnRefresh', function () { Refresh(); }); function Refresh() { var LoanID = $("#LoanID").val(); if (LoanID != "") { document.forms["frmTemps"].submit(); } } &lt;/script&gt; </code></pre> <p>Below, is my pertinent part of the Controller:</p> <pre><code>[HttpPost] public ActionResult GeneratePDF(ViewModelTemplate_Guarantors **model**) { try { int FolderNo, GuarantorNum = 0; string Folder, LoanFolder = String.Empty; string FormId, FormName, GuarantorName = String.Empty; int LoanId = Convert.ToInt32(model.LoanId); LoanFolder = LoanId.ToString().PadLeft(8, '0'); //To calculate FolderId based on LoanId if ((LoanId &gt; 0) &amp;&amp; (LoanId &lt; 99000)) { FolderNo = ((int)(LoanId / 10000) * 10000); } else { FolderNo = ((int)(LoanId / 1000) * 1000); } Folder = ((int)FolderNo).ToString(); Folder = Folder.PadLeft(8, '0'); </code></pre> <p>I submitted form <code>A</code> through button 1. The model came back and populated my <code>View A</code>. Now I want to submit <code>View A</code> again with button 2 to a different Action method in the same controller.</p> <p>When I submit the form again to the same Controller with my Model as the receiving parameter, my model is empty when it gets to the action method. I know my model has data in it when it came back above in the first step.</p> <p>Being relatively new to MVC, I am under the impression that if your View is populated with data and you submit your form to a Controller, you Model should have data in it. I realize now, that the Model is not persistent. Once you pass the Model to the View and vice-versa, it's gone. I get it.</p> <p>All I want to find out is how can I populate my model with data before I submit it to the Controller so it sends data to the Controller with my Model populated to render a View? It's that simple. Not ehtat my model is a strongly typed Model. I feel that this is a major cornerstone to understanding how I can submit data back &amp; forthe between Views &amp; Controllers and is very important to the understanding of how MVC works.</p> <p>What am I doing wrong where my data is empty when it gets to my Controller again. How do I get the data from the View back inside the Model before I submit where my Model will have data in it in order to do some processing and render the View again.</p> <p>If there is data that already resides on the form from the previous postback, then when I post again via the second button, why doesn't MVC use the model binder to create my model with the form data and post it.</p> <p>I know the loanid and ddl won't be in the data (because it's not wrapped around the second form), but I am very perplexed in the way it works. I was expecting it to post the data back that resides on the form into the model. Why doesn't it do this?</p>
 

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