Model对象传递给HttpPost行动null值 [英] Model object passed to HttpPost action is having null values

查看:118
本文介绍了Model对象传递给HttpPost行动null值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有特性的模型中声明,控制器动作。并与视图模型指定的视图。 我填写数据表格并提交,但模型只有所有属性的空值。如果我和视图模型试试我得到HttpPost动作相同空值。
我的模型:

 公共类供应商
{
    公共字符串SupplierSequenceNumber {搞定;组; }    公共字符串SupplierName {搞定;组; }    公共字符串SupplierActive {搞定;组; }
}

我的控制器:

  [HTTPGET]
    公众的ActionResult的Add()
    {
        SupplierVM objSupplierVM =新SupplierVM();
        返回查看(objSupplierVM);
    }    [HttpPost]
    公众的ActionResult添加(供应商objSupplier)
    {
        返回查看();
    }

我的观点:

  @model AIEComm.ViewModel.SupplierVM
 @using(Html.BeginForm(添加,供应商,FormMethod.Post,新{ID =formAddSupplier}))
{
   < D​​IV CLASS =控制组>
            @ Html.LabelFor(M = GT; m.objSupplier.SupplierName,新{@class =控制标签})
             < D​​IV CLASS =控制>
             @ Html.TextBoxFor(M = GT; m.objSupplier.SupplierName,新{占位=斯沃琪风格})
             @ Html.ValidationMessageFor(M = GT; m.objSupplier.SupplierName)
             < / DIV>
           < / DIV>
           < D​​IV CLASS =控制组>
            @ Html.LabelFor(M = GT; m.objSupplier.SupplierActive,新{@class =控制标签})
             < D​​IV CLASS =控制>
             @ Html.DropDownListFor(M = GT; m.objSupplier.SupplierActive,新的SelectList(AIEComm.Models.Utilities.YesNoSelectList,值,文本),新的{@类=})
             @ Html.ValidationMessageFor(M = GT; m.objSupplier.SupplierName)
             < / DIV>
           < / DIV>
           < D​​IV CLASS =控制组>
               < D​​IV CLASS =控制>
                  <输入类型=提交级=BTN BTN-主要ID =btnSubmit按钮值=添加/>
               < / DIV>
           < / DIV>
}


解决方案

这样做的原因是以下code:

  M => m.objSupplier.SupplierName

您要生成一个模型,是一个ViewModel内的HTML元素。这是一个好办法,你的问题可以很容易解决的。

这是一个很好的方法,因为你保持组织的事情,但它不工作,因为上面的code是说:


  

好吧,使用视图模型对象( M ),取 objSupplier 对象,然后使用 SupplierName 属性。


这是好的,但是当你提交的数据,你说(的动作):


  

您好,我有一个 SupplierName 属性。请把这个到 objSupplier 对象,你可以找到视图模型对象中。


要其行为说好吧,我期待一个 objSupplier 的对象,但是这是什么视图模型,你说的?

有一个简单的解决方案是创建一个新的局部视图生成表单。这是模型类型应该是:

_SupplierForm.cshtml

  @model供应商@* // 表格 *@

在你看来,继续使用相同的视图模型,但是传递正确的供应商模式:

  @model AIEComm.ViewModel.SupplierVM@ Html.Partial(_ SupplierForm,Model.objSupplier)

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>
}

解决方案

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)

这篇关于Model对象传递给HttpPost行动null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆