I’ve got a view with a submit formular (Html.BeginForm()), and want to set some model properties in the controller, returning the view, hoping to see this properties in the view.
Here is my pattern on the standard MVC 3 logon formular.
Precision: I really want to set these properties within the POST treatment, not in a GET.
Could you please explain why my properties set in the controller are never displayed on the view?
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
model.RememberMe = true;
model.UserName = "foobar";
return View(model);
}
With a view like the standard LogOn.cshtml:
@using (Html.BeginForm()) {
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="editor-label">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</div>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
}
If you want to use in HttpPost Action than you have to do following way.