ASP.NET MVC 解除绑定操作参数

发布于 2024-08-09 14:33:00 字数 724 浏览 2 评论 0原文

是否可以禁止某个操作参数在请求之间保留其值?

[HttpPost]
public ActionResult MyAction(string value1, string value2)
{
        if(value1=="hi")
             ModelState.AddModelError("value1", "Can't have hi");
        //do stuff
        if(ModelState.IsValid)
           return RedirectToAction("Finish");
        else
           return View()
}


[HttpGet]
public ActionResult MyAction()
{
        return View()
}

该视图由一个带有两个输入框(value1 和 value2)的简单表单组成。一旦提交且验证失败,则返回视图。我希望视图中文本框的值始终为空。

如果模型无效,则保留文本框“value1”的值。

我尝试将文本框声明为 <%= Html.TextBox("value1", null) %>但该值仍然保留。我还尝试使用 [Bind(Exclude="value1")] 但这不适用于单个变量。

更新 2:

我正在为用于验证码(自定义解决方案)输入的文本框执行此操作。我希望在加载页面时清除文本框,但我希望保留验证。

Is it possible to disable a certain action parameter from retaining its value across requests?

[HttpPost]
public ActionResult MyAction(string value1, string value2)
{
        if(value1=="hi")
             ModelState.AddModelError("value1", "Can't have hi");
        //do stuff
        if(ModelState.IsValid)
           return RedirectToAction("Finish");
        else
           return View()
}


[HttpGet]
public ActionResult MyAction()
{
        return View()
}

The view consists of a simple form with two input boxes (value1 and value2). Once submitted and validation fails, the view is returned. I want to always have the value of the textbox in the view to be empty.

The value for the textbox "value1" is retained if the the model is invalidated.

I tried to declare the textbox as <%= Html.TextBox("value1", null) %> but the value is still retained. I also tried to use [Bind(Exclude="value1")] but that dosen't work on a single variable.

Update 2:

I'm doing this for a textbox that is used for Captcha (custom solution) input. I want the textbox to be cleared any time the page is loaded, but I want validation to remain.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隔纱相望 2024-08-16 14:33:00

尝试调用

ModelState["value1"].Value 
  = new ValueProviderResult(null, string.Empty, CultureInfo.InvariantCulture);

在从控制器操作中返回视图之前

。其作用是保留与键“value1”关联的所有错误,但用空值替换该值。

Try calling

ModelState["value1"].Value 
  = new ValueProviderResult(null, string.Empty, CultureInfo.InvariantCulture);

before you return the view from within your controller action.

What this does is keep all the errors associated with the key "value1", but replaces the value with an empty value.

清欢 2024-08-16 14:33:00

您正在做什么导致它被保留? MVC 中没有像 ViewState 这样的东西可以在多个请求中保留一个值,除非您正在编写代码或使用表单字段来实现这一点。

景色如何?这个操作方法是通过 GET 还是 POST 调用的?你的方法中包含的“do stuff”是什么?

编辑:您仍在示例代码中显示//do stuff。这些内容是否包含对 ViewData 的任何引用?您的问题是关于绑定的,但我没有看到任何绑定发生。也许这超出了我的理解范围。

编辑2:很高兴菲尔看到这个!原来的问题没有提到 ModelState。

What are you doing that's causing it to be retained? There isn't anything like ViewState in MVC that will persist a value over multiple requests unless you're writing code or using form fields to make it do so.

What does the view look like? Is this action method being called via GET or POST? What's the "do stuff" contained in your method?

Edit: You're still showing //do stuff in your example code. Does that stuff contain any references to ViewData? Your question is about binding, but I don't see any binding happening. Maybe this is beyond my understanding.

Edit 2: Glad Phil saw this one! The original question didn't mention the ModelState.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文