views:

188

answers:

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.

+2  A: 

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.

Dennis Palmer
Sorry for the vague question. Was up late night and didn't realize how vague it was.
Baddie
Dosen't MVC model binding store an attempted value dictionary? I'm not passing anything to the view, its automatically done.
Baddie
Not sure why I got downvoted for trying to be helpful. I should have at least been given credit for helping the OP edit the question to provide the info that Phil needed to answer it.
Dennis Palmer
+2  A: 

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.

Haacked
Tried that, but it seems to remove any validation done against the textbox. So in the example in my question if I was to supply "hi" in the textbox for "value1" the validation wouldn't be displayed.
Baddie
Update: I'm doing this for a textbox that is used for Captcha (custom solution) input. I want the textbox to be cleared on any time the page is loaded, but I want validation to remain.
Baddie
Ah, good point. Sorry. I updated the answer with what should work.
Haacked
Thanks, that worked.
Baddie
Phil, I'm glad you saw this question. The original version of the question didn't even mention model validation and I was stumped.
Dennis Palmer