tags:

views:

37

answers:

1

I have a Textbox rendered by an HtmlHelper

 <%= Html.TextBox("CategoryTitle",Model.CategoryTitle) %>

I post to an action. In the action I manually change the value for CategoryTitle and need to display this new value to the user, but the original value from the post is taken.

 public ActionResult Textboxer(CategoryViewModel model)
 {

     model.CategoryTitle = model.CategoryTitle + "val1" ;


     return View("Textboxer", model);
 }

I need to keep the default behaviour of the Textbox (getting red when invalid).I dont feel like writing my own helper for it.

Is there an easier way?

+1  A: 
ModelState["CategoryTitle"].Value = 
    new ValueProviderResult(NeededValue, NeededValue, CultureInfo.CurrentCulture);

The reason behind this, I believe, is that Html.TextBox re-use "saved" values from ModelState whenever possible.

queen3
I am not shure if I understood this. I could clear a Textbox with this method, but setting CategoryTitle after clearing did not have any efect. However I could set "RawValue" of ValueProviderResult and the Textbox had the value. Do you think this is the way to go?
Malcolm Frexner
Yes, sorry, you need to set value of the ValueProviderResult. The reason behind this, as far as I understand, is that Html.TextBox() checks if ModelState has "saved" value to re-display. I've updated the answer.
queen3