MVC 3视图没有被模型后刷新提交 [英] MVC 3 The View is not being refreshed after model submit
问题描述
我有一个奇怪的问题,我在MVC 3项目视图。我有一个数据编辑(使用模板创建)的标准视图。当我提交表单,我改变了名称
属性,但我回来从控制器浏览器后,我仍然看到 LOREM
值。为什么呢?
@using(Html.BeginForm())
{
@ Html.EditorFor(型号=> model.Name)
<输入类型=提交值=保存/>
}
公众的ViewResult EditUserData(INT ID)
{
[...]
的UserData模式=新的UserData();
model.Name =排版 返回视图(〜/查看/ UserDetails.cshtml模型);
} [HttpPost]
公众的ViewResult EditUserData(的UserData模型)
{
model.Name =文字;
返回视图(〜/查看/ UserDetails.cshtml模型);
}公共类ControlUserData
{
[...] [需要]
[显示(=的ResourceType typeof运算(资源),名称=UserNameFirst)]
公共字符串名称{;组; }
}
您需要从的ModelState
如果你想修改它在后/获取删除值
[HttpPost]
公众的ViewResult EditUserData(的UserData模型)
{
ModelState.Remove(姓名);
model.Name =文字;
返回视图(〜/查看/ UserDetails.cshtml模型);
}
这是内置的MVC behavoir:所有的 Html.Helpers
$ P $在的ModelState
收集了实际的模型值。
有关于这这里的好文章:<一href=\"http://www.west-wind.com/weblog/posts/2012/Apr/20/ASPNET-MVC-Postbacks-and-HtmlHelper-Controls-ignoring-Model-Changes\">ASP.NET MVC回发和控件的HtmlHelper忽略模式更改。
I have a strange problem with my view in the MVC 3 project. I have a standard view for data editing (created using the template). When I submits the form, I change the Name
property, but after I came back to the browser from the controller I still see the lorem
value. Why ?
@using (Html.BeginForm())
{
@Html.EditorFor(model => model.Name)
<input type="submit" value="Save" />
}
public ViewResult EditUserData(int id)
{
[...]
UserData model = new UserData();
model.Name = "lorem";
return View("~/Views/UserDetails.cshtml", model);
}
[HttpPost]
public ViewResult EditUserData(UserData model)
{
model.Name = "ipsum";
return View("~/Views/UserDetails.cshtml", model);
}
public class ControlUserData
{
[...]
[Required]
[Display(ResourceType = typeof(Resources), Name = "UserNameFirst")]
public string Name { get; set; }
}
You need to remove the value from the ModelState
if you want to modify it in a post/get:
[HttpPost]
public ViewResult EditUserData(UserData model)
{
ModelState.Remove("Name");
model.Name = "ipsum";
return View("~/Views/UserDetails.cshtml", model);
}
This is the built in MVC behavoir: all the Html.Helpers
prefers the values in the ModelState
collection over the actual model values.
There is a good article about this here: ASP.NET MVC Postbacks and HtmlHelper Controls ignoring Model Changes.
这篇关于MVC 3视图没有被模型后刷新提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!