Mon*_*per 10 asp.net-mvc lambda model-binding asp.net-mvc-3
public ActionResult SomeAction(int Id){
//Id is set to 2
var model = //get some thing from db using Id(2);
//Now model.Id is set to 9;
return View(model);
}
----------View----------
@Html.HiddenFor(x => x.Id)
Run Code Online (Sandbox Code Playgroud)
当我查看源时,此隐藏字段设置为2而不是9.如何将其映射到模型而不是映射到URL路由信息?
PS我不想重命名参数因为那时我会丢失我看起来很漂亮的网址,除非我改变路由信息.我已经做到了,它确实有效,但不是我想要的.
nem*_*esv 23
当一个Action被调用的框架构建一个ModelStateCollection基于查询字符串值,后期数据,路由值等等.这ModelStateCollection将被传递给View.在尝试从实际模型中获取值之前,所有HTML输入帮助程序都尝试从第ModelStateCollection 一个获取值.
因为输入模型是输入模型,int id但输出模型是一些新模型,助手将使用ModelStateCollection(来自查询字符串)中的值,因为属性名称Id是匹配的.
要使其工作,您必须ModelStateCollection在将新模型返回到视图之前手动清除:
public ActionResult SomeAction(int Id){
//Id is set to 2
ModelState.Clear();
var model = //get some thing from db using Id(2);
//Now model.Id is set to 9;
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3512 次 |
| 最近记录: |