C# 当以完全相同的方式实现时,Form POST例程对一个操作有效,但对另一个操作无效
我正试图将类型为ProductBrandViewModel的视图模型从我的表单提交到我的控制器操作,但是视图模型上的属性C# 当以完全相同的方式实现时,Form POST例程对一个操作有效,但对另一个操作无效,c#,asp.net-mvc,controller,http-post,html.beginform,C#,Asp.net Mvc,Controller,Http Post,Html.beginform,我正试图将类型为ProductBrandViewModel的视图模型从我的表单提交到我的控制器操作,但是视图模型上的属性name,即使已从表单传递了值,结果也是空的 表格 <div class="modal fade" id="modAddProductBrand" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"&g
name
,即使已从表单传递了值,结果也是空的
表格
<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">×</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>
ProductBrandViewModel
public class ProductBrandViewModel
{
public Guid Id { get; set; }
[Required]
[Display(Name = "Brand Name")]
public string Name { get; set; }
public int ProductCount { get; set; }
}
在create视图中,您正在使用textboxforhelper方法,如
Html.TextBoxFor(model => model.ProductBrand.Name)
这将生成一个名为“ProductBrand.name
”的输入字段
到
或者更新您的视图,使输入字段生成字段名为
name
,而不是ProductBrand.name
,因此我找到了它,因为表单生成的输入字段为ProductBrand.name
,使用brand
作为参数将不起作用,因为它没有ProductBrand.Name
的属性,通过将参数名称从brand
更改为ProductBrand
,MVC能够匹配该参数,并且能够正确地为Name属性赋值
<input name="ProductBrand.Name" type="text" value="" /> .
这正是我的分类操作起作用的原因,因为参数名称是
productCategory
,而不仅仅是Category
您好,谢谢您的回复,但是如果您注意到了,我已经提到,按照您所说的更改该参数会有所帮助。但是在我的问题中有一个部分叫做工作版本,在这里我对类别有完全相同的实现。控制器接受ProductCategoryViewModel而不是ProductCategoryPageViewModel,并且工作正常。为什么当我尝试为品牌实现同样的功能时,它不起作用呢?对于工作版本,您是在发布ajax帖子还是在发布普通形式的帖子?在两个屏幕中检查生成的输入表单名称。它们都是普通表单发布,目前不使用AJAX。
<input name="ProductBrand.Name" type="text" value="" /> .
public ActionResult Create(ProductBrandViewModel brand)
{}
public ActionResult Create(ProductBrandPageViewModel brand)
{}