ExternalLogin() in Asp.net MVC not passing changed viewmodel data correctly

In my ASP.Net MVc site I have a Register action that takes my RegisterViewModel

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
       // register code here
    }

and I have the external login action "ExternalLogin" for sites like Facebook. Both of these actions were part of the ASP.Net MVC template I used.

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult ExternalLogin(RegisterViewModel model, string provider, string returnUrl)
    {
        System.Web.HttpContext.Current.Session["gender"] = model.Gender;
        System.Web.HttpContext.Current.Session["profileType"] = model.ProfileType;
        return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
    }

When I call "Register", my RegisterViewModel data is all good (model.Gender, model.ProfileType, etc.), but when I call "ExternalLogin" Gender and ProfileType aren't correct. They are the default values.

Anyone know why this is?

Here is the html and code in the viewmodel

<div id="formGroup1" class="form-group">
            @Html.LabelFor(m => m.Gender, new { @id = "genderLabel" })
            <div class="">
                @Html.EnumDropDownListFor(m => m.Gender, new {@id = "genderId", @class = "selectpicker form-control" })
            </div>
        </div>
<div id="formGroup2" class="form-group">
            @Html.LabelFor(m => m.ProfileType, new { @id = "profileTypeLabel", @class = "" })
            <div class="">
                @Html.EnumDropDownListFor(m => m.ProfileType, new { @class = "form-control selectpicker" })
            </div>
        </div>

in my viewmodel I have

    [Required()]
    [Display(Name = "I am a")]
    public Gender Gender { get; set; }

    [Required()]
    [Display(Name = "Who wants to")]
    public ProfileType ProfileType { get; set; }

Answers


Your CSHTML code is missing the essential parts which determine whether or not the data gets loaded into the view model, namely the form and buttons.

The data will only get loaded into the view model properly if the ASP input elements are specified within the same form as the button which redirects to your controller.

I am guessing in your scenario, the buttons associated to Register and ExternalLogin are located in two separate forms.

(I also presume you have @model set up correctly at the top of your page.)


Need Your Help

Picasso image caching

android image caching picasso

I want to download the following image downloading code with Picasso image cache.

Change color of input labels with jquery validate

javascript jquery jquery-validate

I'm trying to change the class of my form input labels when a specific field fails to validate. I want to add the 'error' class to the spans directly above any invalid form elements.