CrazyCoderz
CrazyCoderz

Reputation: 1341

Another Question about partial Views

I feel like a total idiot but for the life of me can't figure out what the heck I am missing here.

I have the following:

 @section TopPane

{ @*@{Html.Action("_OpenIDSignInPartial", "Account");}*@

@{Html.RenderAction("_OpenIDSignInPartial", "Account");}

Sign-in with your mysite.com account or Register for an account @{Html.RenderPartial("_LoginPartial");} @{Html.RenderPartial("_RegisterPartial");}

}

As you can see I have 3 Partial Views being rendered. Now for the Controller Code----

public class AccountController : Controller
{
    private readonly IAuthenticationService _authenticationService;
    OpenIdRelyingParty _openIdRelyingParty = new OpenIdRelyingParty(null);
    public AccountController(IAuthenticationService authenticationService)
    {
        _authenticationService = authenticationService;
    }

    public ActionResult Index()
    {
        return View();
    }
    public ActionResult SignIn()
    {
        return View();
    }
    [HttpPost]
    public ActionResult SignIn(AccountSignInViewModel accountSignInViewModel)
    {
        return View();
    }
    public ActionResult OpenIdSignIn()
    {
         AccountIndexViewModel accountIndexViewModel = new AccountIndexViewModel();
        IAuthenticationResponse authenticationResponse = _openIdRelyingParty.GetResponse();
         switch (authenticationResponse.Status)
         {
             case AuthenticationStatus.Authenticated:
                 try
                 {
                     var claimedIdentifier = authenticationResponse.ClaimedIdentifier.ToString();
                     _authenticationService.OpenIdSignIn(claimedIdentifier);
                 }
                 catch (Exception)
                 {
                     // Do something with this man!
                     throw;
                 }
                 break;
             case AuthenticationStatus.Canceled:
                 accountIndexViewModel.openIdSignInViewModel.ErrorMessage = "An Error Message";
                 break;
         }
         return View("SignIn",accountIndexViewModel); // ALWAYS an ERROR
    }
    [HttpPost]
    public ActionResult OpenIdSignIn(AccountOpenIdSignInViewModel accountOpenIdSignInViewModel)
    {
        IAuthenticationResponse authenticationResponse = _openIdRelyingParty.GetResponse();
        if (authenticationResponse == null)
        {
            Identifier identifier;
            if (Identifier.TryParse(accountOpenIdSignInViewModel.openid_identifier, out identifier))
            {
                try
                {
                    IAuthenticationRequest request =
                        _openIdRelyingParty.CreateRequest(accountOpenIdSignInViewModel.openid_identifier);

                    return request.RedirectingResponse.AsActionResult();
                }
                catch (ProtocolException protocolException) // Prolly should add some errors to the model
                {
                    ModelState.AddModelError("openid_identifier","Unable to authenticate");
                    return View("SignIn");
                }
            }
        }
        return View();
    }
    public ActionResult _OpenIDSignInPartial(AccountOpenIdSignInViewModel accountOpenIdSignInViewModel)
    {
        return PartialView("_OpenIdSignInPartial");
    }
}

When I reurn the view from the OpenIdSignIn ActionResult() I get the following error The model item passed into the dictionary is of type 'Web.ViewModels.AccountIndexViewModel', but this dictionary requires a model item of type 'Web.ViewModels.AccountSignInViewModel'. Ok great so I will return a AccountSignInViewModel right?? Then I get an error saying it needs the AccountIndexViewModel... CATCH 22 here.

Upvotes: 0

Views: 167

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

You are returning a AccountIndexViewModel to the main view. This means that the 2 partials must be strongly typed to AccountIndexViewModel:

  • _LoginPartial
  • _RegisterPartial

If they aren't you need to pass the proper view model when rendering them.

As far as _OpenIDSignInPartial is concerned you are rendering it through the _OpenIDSignInPartial action in which you

return PartialView("_OpenIdSignInPartial");

According to the error message it looks like _OpenIdSignInPartial.cshtml is strongly typed to AccountSignInViewModel:

@model AccountSignInViewModel

so make sure that you are passing an instance of this model when returning the partial view:

AccountSignInViewModel signInViewModel = ...
return PartialView("_OpenIdSignInPartial", signInViewModel);

Upvotes: 3

Related Questions