views:

58

answers:

2

Hi Everyone, I have a WYSIWYG editor set up using ASP.NET MVC. When I post, everything is fine, however when I post and there is an error when the page comes back and shows the errors, the value in the textbox is messed up (it has become un-HTML Encoded) and for form validation reasons, this is fine. My question is this - how, in the controller (preferably), can I change the value of the text in the textarea? Even if I use a ViewData["fieldname"] and set the asp code to:

<%= Html.TextArea("fieldname", ViewData["fieldname"], new { id = "fieldthing", name = "fieldthing", cols = "80", rows = "10" })%>

It still doesn't work because that ViewData is the default value which, after the post, isn't returned to that value, it stays whatever was typed.

So any thoughts on how to edit this value? I tried passing in a new view, but that didn't work either.

Thanks!

A: 

When you've detected an error in your Controller, are you resetting the value of ViewData["fieldname"] to be the same value as fieldname?

Consider implementing something like this, if it helps:

if (!SanityCheckUserInput())
{
  //something didn't pass validation. send back to user for correction
  ViewData["fieldname"] = Request.Form["fieldname"];
  return View(); //return the same View
}
p.campbell
A: 

On post, the ModelState will hold the posted value and use that to repopulate the form if returning to the same page (validation failure). If you don't want to do that, try ModelState.Remove("fieldthing");

dotjoe