在 TryUpdateModel 之前更新值提供程序
假设我们有一个具有名为 PetsName 属性的类。如果屏幕上留空,我想更新值提供者,因此如果用户不输入宠物名,我们会强制“未命名”。这不是实际场景..这当然是一个示例,因此“仅在网页上设置默认值等”之类的答案不适合此场景:)
主要问题是我们想要更新值,因此当您更新模型,它将使用您覆盖的任何内容。我想一个想法是删除该值并添加它。当我检查 ModelState 时,它确实具有更新的值,但是当我调用 TryUpdateModel 时,该值并未更新。我下面所做的可能确实是正确的,这里还有另一个问题,但我想我会先尝试这个。谢谢!
<代码>
//Sample case:
[HttpPost]
public ActionResult Edit(PetOwner petOwner)
{
//If pets name is not set, force to "Unknown"
if(petOwner.PetsName=="")
{
//Tried this too ModelState.Remove("PetsName");
//ModelState.Add("PetsName", new ModelState());
ModelState["PetsName"].Value = new ValueProviderResult("Unnamed", "Unnamed", CultureInfo.CurrentCulture);
}
//Get the record/relationships from DB to merge with ModelState
PetOwner petOwnerToSave = from o in ctx.PetOwners where o.PetOwnerId == petOwner.PetOwnerId select o;
TryUpdateModel(petOwnerToSave);
//Save petOwnerToSave
}
<代码>
Lets say we have a class with a property called PetsName. If it is left blank on the screen I want to update the value provider so if the user doesn't enter a pet name, we force 'unnamed'. This isn't the actual scenario.. this is of course a sample, so answers like 'just set default values on a webpage, etc' won't fit this scenario : )
The main issue is we want to update the values so when you update the model it will use whatever you have overridden. I guess one idea is to remove the value and add it. When I check ModelState, it does have the updated value, however when I call TryUpdateModel, the value is not updated. Its possible what Im doing below is indeed correct and there is another issue here but I figured I'd try this first. Thanks!
//Sample case:
[HttpPost]
public ActionResult Edit(PetOwner petOwner)
{
//If pets name is not set, force to "Unknown"
if(petOwner.PetsName=="")
{
//Tried this too ModelState.Remove("PetsName");
//ModelState.Add("PetsName", new ModelState());
ModelState["PetsName"].Value = new ValueProviderResult("Unnamed", "Unnamed", CultureInfo.CurrentCulture);
}
//Get the record/relationships from DB to merge with ModelState
PetOwner petOwnerToSave = from o in ctx.PetOwners where o.PetOwnerId == petOwner.PetOwnerId select o;
TryUpdateModel(petOwnerToSave);
//Save petOwnerToSave
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里幕后的真正问题是 Html.HiddenFor 没有显示正确的值,即使 TryUpdateModel 正在更新模型中的值以提供给视图。
这里的问题是,Html 助手假设如果您在发布后渲染视图,则一定存在错误(否则您将使用 GET 方法重定向回视图 - 因此出现 Post Redirect Get 问题)
这已详细描述在:
http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx< /a>
The real issue behind the scenes here is that Html.HiddenFor wasn't displaying the correct value even though TryUpdateModel was updating a value in the model to give to the view.
The issue here is that the Html helpers assume if you are rendering a view after a post, there mustve been an error (otherwise you wouldve redirected back to the view with a GET method - hence the Post Redirect Get issue)
This is described in detail at:
http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx