Note that there are some explanatory texts on larger screens.

plurals
  1. POStrongly typed ViewModel in POST action method contains only null values
    text
    copied!<p>I am trying to implement my Edit action methods with strongly typed view which receives a custom-shaped ViewModel class. In other words, I want a strongly typed ViewModel which contains the Linq entity that should be edited plus a few other objects that should be displayed in the View. </p> <p>I can see the view when calling the GET Edit action method, but the strongly typed POST action method receives only a ViewModel class with null parameters and I can't figure out how to retrieve the POST parameters. </p> <p>The View Model looks as follows:</p> <pre><code>//my custom-shaped ViewModel public class CustomersFormViewModel { public SelectList AccountTypesDropDownBox; public SelectList CountriesDropDownBox; public Customer Customer; } </code></pre> <p>The action method looks as follows:</p> <pre><code>// // GET: /CustomersController/Edit public ActionResult Edit(int ID) { var model = new CustomersFormViewModel { Customer = repository.Load(ID.Value), CountriesDropDownBox = GetCountries(), AccountTypesDropDownBox = GetAccountTypes() }; return View(model); } // // POST: /CustomersController/Edit [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(CustomersFormViewModel model) { //THE NEXT LINE THROWS!!! Debug.Assert(Model.Customer!=null); return View(model); } </code></pre> <p>And this is my Edit view:</p> <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/CustAdminMaster.master" Inherits="System.Web.Mvc.ViewPage&lt;Zeiterfassung.Controllers.CustomersController+CustomersFormViewModel&gt;" %&gt; &lt;asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"&gt; NewEdit &lt;/asp:Content&gt; &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt; &lt;h2&gt; NewEdit&lt;/h2&gt; &lt;%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %&gt; &lt;% using (Html.BeginForm()) {%&gt; &lt;fieldset&gt; &lt;legend&gt;Fields&lt;/legend&gt; &lt;p&gt; &lt;label for="FirstName"&gt;FirstName:&lt;/label&gt; &lt;%= Html.TextBox("FirstName",Model.Customer.FirstName) %&gt; &lt;%= Html.ValidationMessage("FirstName", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;% } %&gt; &lt;div&gt; &lt;%=Html.ActionLink("Back to List", "Index") %&gt; &lt;/div&gt; &lt;/asp:Content&gt; </code></pre> <p>I've also tried a POST action method with formValues parameters, but the the viewmodel still did not contain the posted parameters:</p> <pre><code>[AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(int ID, FormCollection formValues) { CustomersFormViewModel model = new CustomersFormViewModel(); UpdateModel(model); //THE NEXT LINE STILL THROWS Debug.Assert(model.Customer!=null); return View("NewEdit",model); } </code></pre> <p>The only way I have found so far is to write my own code that grab the posted parameters from the FormCollection and updates my custom ViewModel accordingly. But this approach seems a bit primitive. Is there really no better way to do this?</p> <p><strong>EDIT:</strong> I've just tried a different syntax in the view as tvanfosson suggested, but the problem remains unchanged:</p> <pre><code>&lt;label for="Customer.FirstName"&gt;FirstName:&lt;/label&gt; &lt;%= Html.TextBox("Customer.FirstName") %&gt; &lt;%= Html.ValidationMessage("Customer.FirstName", "*") %&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