Vinzz
Vinzz

Reputation: 4018

ASP MVC 3, how to set model properties in controller?

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>
    }

Upvotes: 2

Views: 11788

Answers (3)

Praveen Prasad
Praveen Prasad

Reputation: 32107

As far as i understand ur problem : add this action to ur controller

   /*no http post*/
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        model = model ?? new LogOnModel();
        model.RememberMe = true;
        model.UserName = "foobar";

        return View(model);
    }

As per ur comment

   [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        ModelState.Clear();
        model.RememberMe = true;
        model.UserName = "foobar";

        return View(model);
    }

Upvotes: 1

Kyeotic
Kyeotic

Reputation: 19847

I think you are confusing what the [HttpPost] flag is doing. This attribute identifies the action as the one to be used when a request is sent to the server as a POST. When you are first looking at the page, the standard request by the browser is a GET. Your POST action should be the one that checks the submitted form's credentials.

[HttpGet]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
    model.RememberMe = true;
    model.UserName = "foobar";

    return View(model);
}

[HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        //Verify input here
    }

Upvotes: 0

dotnetstep
dotnetstep

Reputation: 17485

If you want to use in HttpPost Action than you have to do following way.

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        ModelState.Remove("RememberMe");
        ModelState.Remove("UserName");
        model.RememberMe = true;
        model.UserName = "foobar";
        return View(model);
    }

Upvotes: 4

Related Questions