1

I am using MVC5 ASP.Net with Razor Engine for a view that has following html helper for displaying a textbox and setting its value to ViewBag.SearchText.

When user inputs text into this textbox and view posts back, then the value is set correctly according to value in ViewBag.SearchText, but when in controller's action the ViewBag.SearchText = string.Empty, then textbox still retains its original value rather than an empty string.

Is this how the Html helper for textbox is supposed to behave Or I am missing something, and how could I set the value of textbox to an empty string? This is quite strange since the ViewBag.SearchText value is an empty string when I step through code in Visual Studio 2013.

@Html.TextBox("SearchText", (string)ViewBag.SearchText)

UPDATE 1:

I found that using the code Model.Clear() at start of the controller action solved this problem. However, I found another way to just clear that part of ModelState that is used to populate the textbox after postback. The key in ModelState is the id of the textbox, which is 'SearchText'. The code for this is as below.

ModelState.SetModelValue("SearchText", new ValueProviderResult(null,string.Empty,
                          System.Globalization.CultureInfo.InvariantCulture));
6
  • 1
    What is your model? Does it have a property string SearchText? Commented Sep 7, 2014 at 2:25
  • No. The textbox is not tied to any model. Commented Sep 7, 2014 at 2:25
  • 1
    Are you saying this happens when you return the view after posting back? Commented Sep 7, 2014 at 2:27
  • Yes. So I input a value 12049 into the text box. When control returns to View I see the value of ViewBag.SearchText = "12049". Then I click another button 'Show All' and in my action method I set ViewBag.SearchText = "", but on return to view I can see that the value of ViewBag.SearchText is an empty string but the text box still shows 12049 as its value after view renders. I am debugging in VS 2013. Commented Sep 7, 2014 at 2:31
  • 1
    That's by default (ModelState retains the value on post back). You can try ModelState.Clear() before setting ViewBag.SearchText = ""; although using a view model is a better solution Commented Sep 7, 2014 at 2:39

1 Answer 1

1

That's by default (ModelState retains the value on post back).

You can use ModelState.Clear() before setting ViewBag.SearchText = ""; and returning the view. However use with caution because clearing ModelState can have unexpected results if your using validation

Sign up to request clarification or add additional context in comments.

3 Comments

I can always rely on server-side validation to overcome this, instead of just client-side validation, when clearing the ModelState?
@Sunil, ModelState.Clear(); will clear errors as well, so you need to make sure you check ModelState.IsValid() before clearing.
I found a way to only clear that item in ModelState, which is used to populate the textbox, and it worked. Code for this is under UPDATE 1 of my post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.