C# MVC4 changed view property not changing data on view HTML page
UPDATE: Jacco's suggestion (below) fixed this for me. Ie, ModelState.Clear() is your friend.
I have a controller called ScheduleController with an ActionResult that looks like this:
[HttpPost]
public ActionResult Create(ScheduledCall scheduledcall)
The scheduledCall model object has a property called content. I have a condition in which I would like to alter the value of the content property and return the model to the view for the user to read in the browser. I'm finding that while changing the value of the property is easily done, the view doesn't render the new value to the HTML display. Instead, it shows the content the user submitted.
Example in the controller action:
scheduledcall.Content = "Mark my words!";
ViewBag.message = scheduledcall.Content;
return View(scheduledcall);
On the view, I output @ViewBag.message (which was set in the controller to match the value of the scheduledcall.Content property). The output in the view is "Mark my words!", but the text area for content (@Html.TextAreaFor(model => model.Content)) shows the text the user input, not "Mark my words!".
How do I get the view to display the updated value in the text area?
I think your answer is here
Html.TextBoxFor does not show updated value in POST action
Either the ModelState.Clear(); or the copying of the model should do the trick.
Ran in to this once also, its one of the not so obvious corners of the MVC framework.
Regards, Jacco
Probably not the answer you're looking for.
Instead of returning a rendered view from your POST consider using Post/Redirect/Get pattern.
Post/Redirect/Get (PRG) is a web development design pattern that prevents some duplicate form submissions, creating a more intuitive interface for user agents (users). PRG implements bookmarks and the refresh button in a predictable way that does not create duplicate form submissions.
链接地址: http://www.djcxy.com/p/53962.html上一篇: 查看值不会返回到控制器操作