开发者

ASP.NET MVC - TextArea values

开发者 https://www.devze.com 2023-01-06 16:26 出处:网络
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 m

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 Vie开发者_开发知识库wData 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!


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
}


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");

0

精彩评论

暂无评论...
验证码 换一张
取 消