MVC 模型数据未绑定

MVC Model data is not binding

我已经创建了表格来添加客户。我用 Viewmodel 渲染了客户页面。查看模型 class 如下,

public class CustomerViewModel
{
    public IEnumerable<MemberShipType> MemberShipTypes { get; set; }
    public Customer Customers { get; set; }
}  

public class Customer
{
    [Display(Name ="Customer ID")]
    public int CustomerId { get; set; }

    [Required(ErrorMessage = "Please enter customer name")]
    [StringLength(255)]
    [Display(Name ="Customer Name")]
    public string CustomerName { get; set; }

    public MemberShipType MemberShipType { get; set; }

    [Required(ErrorMessage = "Please select membership type")]
    [Display(Name = "Membership Type")]
    public byte MembershipTypeId { get; set; }
}

public class MemberShipType
{
    [Display(Name ="Membership Id")]
    public byte Id { get; set; }
    [Required]
    [Display(Name = "Subscription Plan")]
    public string Name { get; set; }
}

添加 class 后,我们创建了使用单个模型保存客户表单数据的操作 class(不是 viewModel)

我已经使用 Viewmodel 创建了客户表单来显示会员类型数据。

UI 使用以下代码可以正常渲染。但是,我无法在操作方法中获取模型数据。

如果我直接在动作数据中使用 viewmodel 就好了。该问题需要将所有视图模型 属性 映射到特定模型。

每次映射模型属性需要更多时间。

任何人都知道如何直接使用 entity framework 添加方法与客户模型(不是视图模型)

@using (Html.BeginForm("Save", "Customer", FormMethod.Post))
{
    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(m => m.Customers.CustomerName, htmlAttributes: 
            new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(m => m.Customers.CustomerName, 
                new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(m => m.Customers.CustomerName, "", 
                new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(m => m.Customers.MembershipTypeId, htmlAttributes: 
             new { @class = "control-label col-md-2" })
            <div class="col-lg-10">

                @Html.DropDownListFor(m => m.Customers.MembershipTypeId,
                    new SelectList(Model.MemberShipTypes, "Id", "Name"), 
                    "Please Select", new {@class = "form-control"})

                @Html.ValidationMessageFor(m => m.Customers.MembershipTypeId, 
                    "", 
                    new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-10 col-lg-offset-2">
                <input type="reset" value="Reset" class="btn btn-default" />
                <button type="submit" class="btn btn-primary">Save</button>    
            </div>
        </div>

    </div>
}

下面的动作模型总是return null。

        [System.Web.Mvc.HttpPost]
        public ActionResult Save(Customer customer)
        {
            if (customer.CustomerId == 0)
            { 
              _context.Customer.Add(customer);
              _context.SaveChanges();
            }
        }

我得到一个客户模型为空。如果我通过 customerViewModel 数据即将到来。谁能知道如何直接获取模型中的数据的答案class?

由于您将视图绑定到 CustomerViewModel 的模型,并且您正在使用 HTML 助手 EditorFor(lambda 重载),因此您应该期望 return 中的相同模型 POST。当您使用 LabelFor 和 EditorFor 时,自动命名可能会为您提供类似 "Customers_CustomerName" 的内容,因此它可以将您的视图模型重新组合在一起。

一个解决方案是将保存方法中的预期模型更改为 'CustomerViewModel' 并仅使用 .Customer 属性 来获取数据。

[System.Web.Mvc.HttpPost]
        public ActionResult Save(CustomerViewModel model)
        {
            if (model.Customer.CustomerId == 0)
            { 
              _context.Customer.Add(model.Customer);
              _context.SaveChanges();
            }
        }

另一种选择是手动命名您的输入字段以直接反映 'Customer' 模型的属性,它会将它们映射到 POST 上的 "Customer" 模型。例如,您只需使用 @Html.EditorFor("CustomerName", Model.Customers.CustomerName)

而不是 @Html.LabelFor(m => m.Customers.CustomerName
<div class="form-group">
            @Html.LabelFor(m => m.Customers.CustomerName, htmlAttributes: 
            new { @class = "control-label col-md-2" })
            <div class="col-md-10">
 *********EDIT HERE      --> @Html.TextBox("CustomerName", Model.Customers.CustomerName
                new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(m => m.Customers.CustomerName, "", 
                new { @class = "text-danger" })
            </div>
        </div>

我找到了这个问题的解决方案。问题的原因是,我以相同的名称创建了控制器和模型。因此,我在 Viewmodel 中更改了具有不同别名的模型名称,如下所示,

public class CustomerViewModel 
{ 
    public IEnumerable<MemberShipType> MemberShipTypes 
    { get; set; } 
    public ProductionCustomer productionCustomers 
    { get; set; }
}  

如果我们在控制器中使用模型对象 Get/POST 即使我们使用 ViewModel(多模型)呈现表单,它也会工作。默认情况下,mvc 会将模型识别为 post 形式。