The heavily introduced ViewModel in the POST method contains only null values

I am trying to implement my editing action methods with a strongly typed view that gets a custom view of the ViewModel class. In other words, I want a strongly typed ViewModel that contains a Linq object that needs to be edited, as well as several other objects that need to be displayed in the view.

I see the view when I call the GET Edit action method, but the strict action of the POST method only gets the ViewModel class with zero parameters, and I can't figure out how to get the POST parameters.

The view model is as follows:

//my custom-shaped ViewModel public class CustomersFormViewModel { public SelectList AccountTypesDropDownBox; public SelectList CountriesDropDownBox; public Customer Customer; } 

The action method is as follows:

 // // 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); } 

And this is my edit:

 <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/CustAdminMaster.master" Inherits="System.Web.Mvc.ViewPage<Zeiterfassung.Controllers.CustomersController+CustomersFormViewModel>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> NewEdit </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2> NewEdit</h2> <%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %> <% using (Html.BeginForm()) {%> <fieldset> <legend>Fields</legend> <p> <label for="FirstName">FirstName:</label> <%= Html.TextBox("FirstName",Model.Customer.FirstName) %> <%= Html.ValidationMessage("FirstName", "*") %> </p> <p> <input type="submit" value="Save" /> </p> </fieldset> <% } %> <div> <%=Html.ActionLink("Back to List", "Index") %> </div> </asp:Content> 

I also tried the POST action method with formValues ​​parameters, but the view model still did not contain the published parameters:

 [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); } 

The only way I've found so far is to write custom code that captures published parameters from FormCollection and accordingly updates my own ViewModel. But this approach seems a bit primitive. Is there a better way to do this?

EDIT: I just tried a different syntax in the view, as tvanfosson suggested, but the problem remains the same:

 <label for="Customer.FirstName">FirstName:</label> <%= Html.TextBox("Customer.FirstName") %> <%= Html.ValidationMessage("Customer.FirstName", "*") %> 
+7
asp.net-mvc
source share
3 answers

You need to specify your fields according to the prefixes in the model. In addition, you will need to modify your view model to use properties instead of fields. By default, binding only models the public properties of the model when binding.

  <label for="Customer.FirstName">FirstName:</label> <%= Html.TextBox("Customer.FirstName") %> <%= Html.ValidationMessage("Customer.FirstName", "*") %> 

Thus, the model’s middleware knows how to associate the form parameter with the corresponding component and the associated property of your model.

+7
source share

It was a while, but in fact there was no answer to this question.

The same problem affected until I caught the auto property on my view model, which has a private set.

Looking at your viewing model, you are missing the auto properties that you get and install together!

So, change your view mode accordingly:

 public class CustomersFormViewModel { public SelectList AccountTypesDropDownBox {get; set;} public SelectList CountriesDropDownBox {get; set;} public Customer Customer {get; set;} } 
+6
source share

Try

 UpdateModel(model, "Customer"); 

And also look here:

UpdateModel prefix - ASP.NET MVC

If it still does not work, set a breakpoint after UpdateModel. If this does not help, try running quickWatch (ctrl + alt + q) with various combinations of calling UpdateModel.

If it still does not work, connect the source and debug a little :). You will learn something new, and then you will tell us about it :). Then everyone is happy.

Good luck.

0
source share

All Articles