TextBoxFor助手保留previous价值,即使模型值为空 [英] TextBoxFor Helper retains previous value even when model value is empty

查看:81
本文介绍了TextBoxFor助手保留previous价值,即使模型值为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有添加一个简单的实体的MVC形式。我使用TextBoxFor(型号=> model.FieldName)创建输入字段。我有一个保存按钮,一个保存并新建按钮。保存并新建按钮应该回发到保存动作,然后用空模型返回当前视图,以便用户添加另一个实体。然而,会发生什么情况,尽管该模型确实是空的,被与用于previous实体输入值产生的输入字段。希望这是有道理的。

I have an MVC form for adding a simple entity. I am using TextBoxFor(model => model.FieldName) to create the input fields. I have a Save button and a Save and New button. The Save and New button is supposed to post back to the Save action and then return the current View with an empty model to enable the user to add another entity. However, what happens is that, even though the model is indeed empty, the input fields are being generated with the values entered for the previous entity. Hope this makes sense.

我知道我可以做一个重定向,但是,似乎是一个丑陋的解决方法,所以如果有人之前已经运行到这一点,我真的AP preciate一些输入。

I know I could do a redirect, but that seems like an ugly workaround, so if anyone has run into this before, I'd really appreciate some input.

感谢。

推荐答案

这里的问题是,你的 ViewData.ModelState 仍填入值从原来的岗位,即使模式为空,你不明确地传递任何值到您的视图。

The issue here is that your ViewData.ModelState is still populated with the values from the original post, even if the Model is null and you don't explicitly pass any values into your view.

其实,我不认为重定向到原来的动作是丑陋的解决方案这一点,但如果你不想这样做,然后清除出的ViewData 应为你工作:

I actually don't think redirecting to the original action is that ugly of a solution, but if you don't want to do that then clearing out the ViewData should work for you:

[HttpPost]
public ActionResult Save(TestModel model)
{            
    ViewData = null;
    return View();
}

这篇关于TextBoxFor助手保留previous价值,即使模型值为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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