[英]Why does posting to this ASP.net MVC post to controller result in a null model?
In the following code, the Product object is always getting set to null - but if I debug, I can see that the values are being posted correctly. 在以下代码中,Product对象始终设置为null-但是,如果我进行调试,则可以看到这些值已正确发布。 What's going on?
这是怎么回事?
Model: 模型:
public class Product {
public string Name { get; set; }
public string Model { get; set; }
}
Controller: 控制器:
[HttpPost]
public ActionResult NewProduct(Product model) {
if (model == null) { throw new NullReferenceException("model is null D:"); }
// Do other things
return View();
}
View: 视图:
@using (Html.BeginForm("NewProduct", "Home")) {
<table>
<tr>
<td>Name</td>
<td>@Html.EditorFor(model => model.Name)</td>
</tr>
<tr>
<td>Model</td>
<td>@Html.EditorFor(model => model.Model)</td>
</tr>
</table>
<input type="submit" value="Add" />
}
The parameter of the controller method cannot be named the same as any of the properties of the model or the binder won't bind properly 控制器方法的参数名称不能与模型的任何属性相同,否则绑定器将无法正确绑定
[HttpPost]
public ActionResult NewProduct(Product newProduct) {
if (newProduct == null) { throw new NullReferenceException("model is null D:"); }
// Do other things
return View();
}
Note: this comparison is case insensitive - here the parameter was model
and the object had a Model
property but it still bugged out. 注意:此比较不区分大小写-这里的参数是
model
,对象具有Model
属性,但仍然存在问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.