-1

I am using Asp.Net identity logic module for authentication process. I am using this theme for login and signup and external logins all in one view.

Here is my Login.cshtml view that contain social login, register and login partials

    @using Helping.ViewModels

@{
    ViewBag.Title = "Log in";
}
<div class="container">
    <div class="row">
        <br />
        <br />
        <div class="col-lg-4">
            <div>
                <section id="socialLoginForm">
                    @Html.Partial("_ExternalLoginsListPartial", new ExternalLoginListViewModel { Action = "ExternalLogin", ReturnUrl = ViewBag.ReturnUrl })
                </section>
            </div>
        </div>
        <div class="col-lg-4">

            <h2><b>Sign Up</b></h2>
            <hr />
        @Html.Partial("Register")
   </div>
        <div class="col-lg-4">
            <h2><b>Log In</b></h2>
            <hr />
            @Html.Partial("LocalLogin")


        </div>
    </div>
</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

LocalLogin and Register are the strongly typed partial views.Problem is that when I try to Login with a user that doesnot exist it returns the model , here is the action

 public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            var user = await UserManager.FindAsync(model.UserName, model.Password);
            if (user != null)
            {
                if (!await UserManager.IsEmailConfirmedAsync(user.Id))
                {
                    string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account-Resend");

                    ViewBag.errorMessage = "You must have a confirmed email to log on.";
                    return View("Error");
                }
                else
                {
                    await SignInAsync(user, model.RememberMe);

                    return RedirectToLocal(returnUrl);
                }
            }
            else
            {
                ModelState.AddModelError("", "Invalid username or password.");
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

It return following error The model item passed into the dictionary is of type 'Helping.ViewModels.LoginViewModel', but this dictionary requires a model item of type 'Helping.ViewModels.RegisterViewModel'.

My Register view expects RegisterViewModel and My LocalLogin expects LoginViewModel

 @using Helping.ViewModels
    @model LoginViewModel

@model HelpingHands.ViewModels.RegisterViewModel

How to provide both the models with one view need help ?

Ghazanfar Khan
  • 3,648
  • 8
  • 44
  • 89
  • You have not provided the sufficient information (you have not even indicated what the model is in the view you have shown) but the error message is self explanatory. Best guess is that `@Html.Partial("Register")` expects a model which is `RegisterViewModel` –  Aug 28 '15 at 22:12
  • Then it would need to be `@Html.Partial("Register", new RegisterViewModel())` - if you don't specify the model to pass to the partial, it will pass the current model which is `LoginViewModel` (hence the error). But why would you have a 'Register' form on the same page as a 'Login' form? A 'Register' action is a once off - once a user has registered they would never need it again so your just degrading your app (and confusing the user) by rendering unnecessary extra html –  Aug 29 '15 at 00:27

1 Answers1

1

You need to combine both your LoginViewModel and RegisterViewModel into one model (ViewModel). Like..

public class LoginRegisterModel
{
   public LoginViewModel LoginModel {get;set;}
   public RegisterViewModel RegisterModel {get;set;}
}

Then you can pass this viewModel to your view..

  @model Helping.ViewModels.LoginRegisterModel

Which says this view will be using the class LoginRegisterModel as the model.. And inside the view for your two partial views you can use..

Html.Partial("LocalLogin", Model.LoginModel)      
Html.Partial("Register", Model.RegisterModel)

The error your getting is because you are not passing any model to your Register view and by default the model passed to your main view is carried forward to the call of partial Register view.

** I m on mobile, forgive me for bad formatting. And if anyone can format the code section its greatly appreciated**

Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59