2011-05-27 99 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