Asp.net MVC ModelState.Clear [英] Asp.net MVC ModelState.Clear
问题描述
谁能给我的ModelState在Asp.net MVC中的角色(或一个链接)的简洁的定义。我特别需要有必要或适宜什么情况下叫 ModelState.Clear()
。
位开放式呵呵 ...很抱歉,我想这可能帮助,如果告诉你,我实际上可以做的:
我编辑的一个叫做页控制器的动作。当我第一次看到的形式来改变页面的细节一切加载了罚款(绑定到一个MyCmsPage对象)。然后我点击产生的MyCmsPage对象的字段中的值按钮( MyCmsPage.SeoTitle
)。它产生罚款和更新对象,我再与新修改的页面对象返回作用的结果,并希望相关文本框(使用渲染<%= Html.TextBox(seoTitle,page.SeoTitle) %方式>
)进行更新......但可惜它显示从已加载的旧模式值
我用 ModelState.Clear()
周围的工作,但我需要知道它是如何工作的,为什么/我不只是做盲目的。
的PageController:
的[AcceptVerbs(POST)]
公众的ActionResult编辑(MyCmsPage页,串提交按钮)
{
//添加seoTitle到当前页面对象
page.GenerateSeoTitle(); //我为什么一定要这样做呢?
ModelState.Clear(); //返回修改后的页面对象
返回查看(页);
}
.aspx的:
<%@页面语言=C#的MasterPageFile =〜/查看/共享/的Site.Master继承=System.Web.Mvc.ViewPage< MyCmsPage>中%GT;
....
< DIV CLASS =C>
<标签=seoTitle>
徐标题< /标签>
&所述;%= Html.TextBox(seoTitle,page.SeoTitle)%GT;
<输入类型=提交值=生成徐标题NAME =提交按钮/>
< / DIV>
我觉得是MVC中的错误。我挣扎着这个问题今天小时。
有鉴于此:
公开的ViewResult SomeAction(SomeModel模型)
{
model.SomeString =一些价值;
返回查看(模型);
}
该视图与渲染的原始模型,忽略了变化。所以我想,也许它并没有使用相同型号的喜欢我,所以我想是这样的:
公开的ViewResult SomeAction(SomeModel模型)
{
newModel,并向VAR =新SomeModel {SomeString =一些价值};
返回查看(newModel,并向);
}
和仍然认为呈现的原始模型。有什么奇怪的是,当我把一个断点在视图和检查模型,它有改变的值。但响应流具有旧值。
最后,我发现了同样的工作围绕着你做:
公开的ViewResult SomeAction(SomeModel模型)
{
newModel,并向VAR =新SomeModel {SomeString =一些价值};
ModelState.Clear();
返回查看(newModel,并向);
}
按预期工作。
我不认为这是一个功能,是吧?
Can anyone give me a succinct definition of the role of ModelState in Asp.net MVC (or a link to one). In particular I need to know in what situations it is necessary or desirable to call ModelState.Clear()
.
Bit open ended huh... sorry, I think it might help if tell you what I'm acutally doing:
I have an Action of Edit on a Controller called "Page". When I first see the form to change the Page's details everything loads up fine (binding to a "MyCmsPage" object). Then I click a button that generates a value for one of the MyCmsPage object's fields (MyCmsPage.SeoTitle
). It generates fine and updates the object and I then return the action result with the newly modified page object and expect the relevant textbox (rendered using <%= Html.TextBox("seoTitle", page.SeoTitle)%>
) to be updated ... but alas it displays the value from the old model that was loaded.
I've worked around it by using ModelState.Clear()
but I need to know why / how it has worked so I'm not just doing it blindly.
PageController:
[AcceptVerbs("POST")]
public ActionResult Edit(MyCmsPage page, string submitButton)
{
// add the seoTitle to the current page object
page.GenerateSeoTitle();
// why must I do this?
ModelState.Clear();
// return the modified page object
return View(page);
}
Aspx:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyCmsPage>" %>
....
<div class="c">
<label for="seoTitle">
Seo Title</label>
<%= Html.TextBox("seoTitle", page.SeoTitle)%>
<input type="submit" value="Generate Seo Title" name="submitButton" />
</div>
I think is a bug in MVC. I struggled with this issue for hours today.
Given this:
public ViewResult SomeAction(SomeModel model)
{
model.SomeString = "some value";
return View(model);
}
The view renders with the original model, ignoring the changes. So I thought, maybe it does not like me using the same model, so I tried like this:
public ViewResult SomeAction(SomeModel model)
{
var newModel = new SomeModel { SomeString = "some value" };
return View(newModel);
}
And still the view renders with the original model. What's odd is, when I put a breakpoint in the view and examine the model, it has the changed value. But the response stream has the old values.
Eventually I discovered the same work around that you did:
public ViewResult SomeAction(SomeModel model)
{
var newModel = new SomeModel { SomeString = "some value" };
ModelState.Clear();
return View(newModel);
}
Works as expected.
I don't think this is a "feature," is it?
这篇关于Asp.net MVC ModelState.Clear的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!