2011-05-27 97 views
0

我覺得自己像個白癡,但對於我的生活卻無法弄清楚我在這裏失去了什麼。關於局部視圖的另一個問題

我有以下幾點:

@section TopPane 

{ @ * @ {Html.Action( 「_ OpenIDSignInPartial」, 「賬戶」);} * @

@ {Html.RenderAction(「_ OpenIDSignInPartial 」, 「賬戶」);}

登錄與您mysite.com帳戶或註冊一個帳號 @ {Html.RenderPartial( 「_ LoginPartial」);} @ {Html.RenderPartial( 「_ RegisterPartial」);}

}

正如你可以看到我已經被渲染3個部分景色。 現在的控制器代碼----

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"); 
    } 
} 

當我reurn從OpenIdSignIn的ActionResult()認爲我收到以下錯誤 傳遞到字典的模型項的類型爲「Web.ViewModels.AccountIndexViewModel ',但是這個字典需要一個'Web.ViewModels.AccountSignInViewModel'類型的模型項。 好吧,所以我會返回一個的InputInModel對嗎?然後我得到一個錯誤,說它需要AccountIndexViewModel ... CATCH 22這裏。

回答

3

您正在返回AccountIndexViewModel到主視圖。這意味着,2個諧音必須是強類型到AccountIndexViewModel

  • _LoginPartial
  • _RegisterPartial

如果不是你需要渲染時他們通過適當的視圖模型。

至於_OpenIDSignInPartial而言你是使其通過_OpenIDSignInPartial行動中,你

return PartialView("_OpenIdSignInPartial"); 

根據錯誤信息,它看起來像_OpenIdSignInPartial.cshtml是強類型到AccountSignInViewModel

@model AccountSignInViewModel 

因此請確保在返回部分視圖時傳遞此模型的實例:

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

甜,讓我試試看,然後馬上回來。一如既往感謝Darin。 – CrazyCoderz 2011-05-28 13:34:28

+0

作品偉大的先生。你太棒了。 – CrazyCoderz 2011-05-28 13:37:28