Form POST 例程在以完全相同的方式实现时对一个动作有效,但对另一个动作无效

Form POST routine works on one action but not the other when implemented exactly the same way

我正在尝试从我的表单向控制器操作提交 ProductBrandViewModel 类型的视图模型,但是视图模型上的 属性 name即使从表单传入了一个值,最终还是 null。

表格

<div class="modal fade" id="modAddProductBrand" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">Add Brand</h4>
            </div>
            <div class="modal-body">
                @using (Html.BeginForm("Create", "ProductBrands", FormMethod.Post, new {id = "frmProductBrand"}))
                {
                    @Html.AntiForgeryToken()
                    @Html.ValidationSummary(false, "You have not entered a name for your brand", new {@class="alert alert-danger"})
                    <div class="form-group">
                        @Html.LabelFor(model => model.ProductBrand.Name)
                        @Html.TextBoxFor(model => model.ProductBrand.Name, null, new {id = "txtProductBrandName", @class = "form-control", placeholder = "Brand Name"})
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-success" id="btnCreateProductBrand"> Save Changes</button>
                    </div>
                }
            </div>
        </div>
    </div>
</div>

POST 动作

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(ProductBrandViewModel brand)
    {
        if (!ModelState.IsValid) return null;
        try
        {
            var userSessionViewModel = GetUserSession();
            var createdProductBrand = CreateProductBrand(userSessionViewModel.Business, brand);
            return RedirectToAction("Index");
        }
        catch
        {
            return View("Index");
        }
    }

ProductBrandViewModel

public class ProductBrandViewModel
{
    public Guid Id { get; set; }

    [Required]
    [Display(Name = "Brand Name")]
    public string Name { get; set; }

    public int ProductCount { get; set; }
}

在您的创建视图中,您正在使用 TextBoxFor 辅助方法,例如

Html.TextBoxFor(model => model.ProductBrand.Name)

这将生成一个名为“ProductBrand.Name”的输入字段

<input  name="ProductBrand.Name" type="text" value="" /> . 

但是在您的 HttpPost 操作中,参数是 ProductBrandViewModel 类型,它没有任何名称为 ProductBrand.Name.

的属性

解决方案是将 HttpPost 操作的参数类型从

更改为
public ActionResult Create(ProductBrandViewModel brand)
{}

public ActionResult Create(ProductBrandPageViewModel brand)
{}

或者更新您的视图,使输入字段生成字段名称为 Name 而不是 ProductBrand.Name

所以我想通了,因为表单生成一个输入字段 ProductBrand.Name,使用 brand 作为参数将不起作用,因为它没有 属性 with ProductBrand.Name,通过将参数名称从 brand 更改为 productBrand,MVC 能够匹配它并能够正确地将值分配给名称 属性。

这正是我的分类操作起作用的原因,因为参数名称是 productCategory 而不仅仅是 category.