模型属性设置控制器没有出现在视图了 [英] Model Property set in controller not showing up in view

查看:109
本文介绍了模型属性设置控制器没有出现在视图了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个pretty的简单例子。

This is a pretty simple example.

控制器

[httppost]
public ActionResult MyPage(MyModelClass myModel) {
    myModel.someProperty = "b";
    return view(myModel)
}

键入的视图包含一个隐藏字段someProperty

The typed view contains a hidden field for someProperty

@Html.HiddenFor(model => model.someProperty)

在获取控制器设置someProperty为A

The Get controller sets someProperty to "a"

然而,一个帖子后someProperty在视图中的值仍然是一个

Yet after a post the value of someProperty in the view is still "a"

这似乎并没有分辩给我。任何见解? THX

That doesn't seem rigth to me. Any insights? Thx

推荐答案

的ModelState 保持贴值A。而且因为它在填充模型值(比模型本身)具有更高的优先级,观点呈现一。你可以从ModelState中删除的价值,并让使用模型值

ModelState keeps the posted value 'a'. And as it has higher priority in populating model values (than the model itself), view renders 'a'. You could remove value from ModelState and let model value be used

[HttpPost]
public ActionResult MyPage(MyModelClass myModel) {
    myModel.someProperty = "b";

    ModelState.Remove("someProperty");    

    return view(myModel);
}

这篇关于模型属性设置控制器没有出现在视图了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆