State and ASP.NET MVC3

I have a simple asp.net MVC 3 application with a page that has the form and @ Html.Hidden ("hiddenField"). I change the value of hiddenField through JavaScript when the form is submitted depending on the input, which I may have to return to the same form, but when I make a hidden field, it is pre-populated with values ​​from the message.

How is this possible. I understand that the programmer is solely responsible for maintaining state with asp.net MVC. or is a helper class that automatically sets the base of values ​​to query values?

+3
source share
3 answers

, , HTML- ModelState . , POST, , . :

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    ModelState.Remove("SomePropertyThatYouWantToModify");
    model.SomePropertyThatYouWantToModify = "some new value";
    return View(model);
}

@Html.HiddenFor(x => x.SomePropertyThatYouWantToModify) .

, ViewData ( , ):

[HttpPost]
public ActionResult Index(FormCollection form)
{
    ModelState.Remove("SomePropertyThatYouWantToModify");
    ViewData["SomePropertyThatYouWantToModify"] = "some new value";
    return View();
}

@Html.Hidden("SomePropertyThatYouWantToModify") .

+11

, . , @Html.Hidden("hiddenField"), , , , , , ( , ).

hiddenField .

0

, , MVC 3. , <h3>QuestionType: @Model.QuestionType</h3>, . . @Html.HiddenFor(m => m.QuestionType);

. post, View(getNewModel())

, . ModelState.Clear(), , .

0

Source: https://habr.com/ru/post/1795313/


All Articles