Model object passed to HttpPost action is having null values

匆匆过客 提交于 2020-01-14 06:05:09

问题


I have a model with properties declared, Controller actions. and View with Viewmodel specified. I fill data in the form and submit, but model has only null values for all properties. If i try with view model i get same null values in HttpPost action. My Model:

public class Supplier
{
    public string SupplierSequenceNumber { get; set; }

    public string SupplierName { get; set; }

    public string SupplierActive { get; set; }
}

My Controller:

[HttpGet]
    public ActionResult Add()
    {
        SupplierVM objSupplierVM = new SupplierVM();
        return View(objSupplierVM);
    }

    [HttpPost]
    public ActionResult Add(Supplier objSupplier)
    {
        return View();
    }

My View:

 @model AIEComm.ViewModel.SupplierVM
 @using (Html.BeginForm("Add", "Supplier", FormMethod.Post, new { id = "formAddSupplier" }))
{
   <div class="control-group">
            @Html.LabelFor(m=>m.objSupplier.SupplierName, new{@class = "control-label"})
             <div class="controls">
             @Html.TextBoxFor(m => m.objSupplier.SupplierName, new { placeholder = "Swatch Style" })
             @Html.ValidationMessageFor(m=>m.objSupplier.SupplierName)
             </div>
           </div>
           <div class="control-group">
            @Html.LabelFor(m=>m.objSupplier.SupplierActive, new{@class = "control-label"})
             <div class="controls">
             @Html.DropDownListFor(m=>m.objSupplier.SupplierActive,new SelectList(AIEComm.Models.Utilities.YesNoSelectList,"Value","Text"),new{@class=""})
             @Html.ValidationMessageFor(m=>m.objSupplier.SupplierName)
             </div>
           </div>
           <div class="control-group">
               <div class="controls">
                  <input type="submit" class="btn btn-primary" id="btnSubmit" value="Add"/>
               </div>
           </div>
}

回答1:


The reason for this is the following code:

m => m.objSupplier.SupplierName

You're generating HTML elements with a model that is inside a ViewModel. This is a good approach, and your problem can be solved quite easily.

It's a good approach because you're keeping things organised, but it's not working because the above code is saying:

Ok, using the ViewModel object (m), take the objSupplier object and then use the SupplierName property.

This is fine, but then when you're submitting data, you're saying (to the action):

Hi, I have a SupplierName property. Please put this into the objSupplier object which you can find inside the ViewModel object.

To which the action says "Well, I am expecting an objSupplier object, but what's this ViewModel you speak of?"

A simple solution is to create a new partial view to generate your form. It's model type should be:

_SupplierForm.cshtml

@model Supplier

@* // The Form *@

In your View, continue to use the same ViewModel, but pass in the correct supplier model:

@model AIEComm.ViewModel.SupplierVM

@Html.Partial("_SupplierForm", Model.objSupplier)


来源:https://stackoverflow.com/questions/18372439/model-object-passed-to-httppost-action-is-having-null-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!