0

So I've tried to follow multiple threads on here to do what I am to, and so far it all seems to have something to do with a singleton which I think is a bit over-the-top solution for my issue.

I'm trying to do the following: I have my login async task which is executed when the user logins in a partial view, then it takes a LoginViewModel as a parameter, when succesfully logged in this happens:

case SignInStatus.Success:
return RedirectToLocal(returnUrl);

Afterwards when my users click into the Member section as follows:

@Html.ActionLink("Medlemsområde", "LoggedIn", "Home", null, new { @class = "login-field btn btn-primary" })

My home controller looks like this

public ActionResult LoggedIn()
{
    if (User.Identity.IsAuthenticated)
        return View("Dashboard");
    else
        return View("Login");
}

My dashboard looks like this

@{
    Html.RenderAction("Index", "Manage");
 }

And then I have my Manage controller

public async Task<ActionResult> Index(ManageMessageId? message)
{
            ViewBag.StatusMessage =
                message == ManageMessageId.ChangePasswordSuccess ? "Dit password er blevet ændret."
                : message == ManageMessageId.SetPasswordSuccess ? "Dit password er gemt."
                : message == ManageMessageId.SetTwoFactorSuccess ? "Din to-faktor godkendelse er gemt."
                : message == ManageMessageId.Error ? "Der skete en fejl."
                : message == ManageMessageId.AddPhoneSuccess ? "Dit telefon nummer blev tilføjet."
                : message == ManageMessageId.RemovePhoneSuccess ? "Dit telefon nummer blev fjernet."
                : "";

            var userId = User.Identity.GetUserId();
            var model = new IndexViewModel
            {
                HasPassword = HasPassword(),
                PhoneNumber = await UserManager.GetPhoneNumberAsync(userId),
                TwoFactor = await UserManager.GetTwoFactorEnabledAsync(userId),
                Logins = await UserManager.GetLoginsAsync(userId),
                BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(userId)
            };
            return View(model);
}

Whenever I have

Html.RenderAction("Index", "Manage");

I get the following error:

The model item passed into the dictionary is of type 'ClientSideProgramming.Models.IndexViewModel', but this dictionary requires a model item of type 'ClientSideProgramming.Models.LoginViewModel'.

How can I fix this without ending up with either passing models all around, or having a singleton?

TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
  • Look at the error message! There is clear instruction what have to do! – TanvirArjel Sep 18 '18 at 15:38
  • @TanvirArjel but how can I solve it? I know what the error is, and I've tried a thousand things to fix it. But I'm unsure how to pass the correct model to it? – guitarherokk Sep 18 '18 at 17:12
  • Hope I shall help you tomorrow..Its midnight in my Country. – TanvirArjel Sep 18 '18 at 17:15
  • The model defined on the Index.cshtml page should be of type IndexViewModel. – Sandhya Sep 18 '18 at 19:59
  • @Sandhya I already got the following at the top of my Index.cshtml, "@model ClientSideProgramming.Models.IndexViewModel" – guitarherokk Sep 18 '18 at 21:17
  • I got it working by putting an if statement before my partial login view in the layout, so whenever dashboard is loaded and logged in,it'll function correctly. "@{ if (!User.Identity.IsAuthenticated) { Html.RenderPartial("Login"); }" – guitarherokk Sep 19 '18 at 08:35

1 Answers1

0

I think you should probably do

return RedirectToAction("Index");

instead of return View("Dashboard") when you are successfully logged in.

Sandhya
  • 606
  • 1
  • 7
  • 13
  • The entire point is that I'm trying to include the Index as a partial view inside my dashboard so I'm able to use a JavaScript to have the index aswell as other pages display at once – guitarherokk Sep 18 '18 at 23:02