部分观点的另一个问题

时间:2011-05-27 15:57:44

标签: asp.net-mvc asp.net-mvc-3 c#-4.0

我觉得自己像个白痴,但是因为我的生活无法弄清楚我在这里失踪的是什么。

我有以下内容:

 @section TopPane

{ @ * @ {Html.Action(“_ OpenIDSignInPartial”,“Account”);} * @

@ {Html.RenderAction(“_ OpenIDSignInPartial”,“Account”);}

使用您的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");
    }
}

当我从OpenIdSignIn ActionResult()重新生成视图时,出现以下错误 传递到字典中的模型项的类型为“Web.ViewModels.AccountIndexViewModel”,但此字典需要“Web.ViewModels.AccountSignInViewModel”类型的模型项。 好的,所以我会返回一个AccountSignInViewModel吗?然后我得到一个错误,说它需要AccountIndexViewModel ... CATCH 22这里。

1 个答案:

答案 0 :(得分:3)

您正在向主视图返回AccountIndexViewModel。这意味着2个部分必须强类型为AccountIndexViewModel

  • _LoginPartial
  • _RegisterPartial

如果不是,则需要在渲染时传递正确的视图模型。

_OpenIDSignInPartial而言,您正在通过_OpenIDSignInPartial行动进行渲染

return PartialView("_OpenIdSignInPartial");

根据错误消息,_OpenIdSignInPartial.cshtml强烈输入AccountSignInViewModel

@model AccountSignInViewModel

因此请确保在返回局部视图时传递此模型的实例:

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