When MVC launches ActionMethod
, it populates the dictionary ModelState
and uses it ModelBinder
to assemble parameters ActionMethod
, if any. He does this both for GET
and for POST
. Which makes sense.
After successful execution, the ActionMethod
view is displayed using a razor, which in my case uses as many calls as possible HtmlHelper
. Until now, you might be thinking, "Yes, I know how MVC works." Wait, I'm getting there.
When we use, for example, @Html.TextFor(m => m.Name)
MVC uses the code that may be here to render the tag.
The interesting part is at the end of the file, where we find:
switch (inputType)
{
case InputType.CheckBox:
case InputType.Radio:
case InputType.Password:
default:
string attemptedValue =
(string)htmlHelper.GetModelStateValue(fullName, typeof(string));
tagBuilder.MergeAttribute("value", attemptedValue ??
((useViewData)
? htmlHelper.EvalString(fullName, format)
: valueParameter), isExplicitValue);
break;
}
, ModelState
Model
. POST
, , , , MVC , . , StackOverflow , . POST
, ,
GET
! .
:
public ActionResult GetCopy(Guid id)
{
var originalModel = this.repository.GetById(id);
var copiedModel = new SomeModel
{
Id = Guid.NewGuid(),
Name = originalModel.Name,
};
return this.View(copiedModel);
}
:
@Html.TextBoxFor(m => m.Id)
@Html.TextBoxFor(m => m.Name)
, GUID
Id
. , Id
action GET
, ModelState
Id
.
, , :
ActionMethod
-ModelState
ActionResult
Modelstate.Clear()
ModelState
Model
.
:
GET
POST
. - ModelState
Model
GET
.