问题描述
|
我觉得自己完全是个白痴,但对于我的一生,我无法弄清楚我在这里错过了什么。
我有以下几点:
@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\");
}
}
当我从OpenIdSignIn ActionResult()遍历视图时,出现以下错误
传递到字典中的模型项的类型为\'Web.viewmodels.AccountIndexviewmodel \',但是此字典要求使用模型项'\ Web.viewmodels.AccountSignInviewmodel \'。
好的,好的,我将返回一个AccountSignInviewmodel对吗?然后我收到一条错误消息,说它需要AccountIndexviewmodel ...在这里接22。
解决方法
您正在向主视图返回
AccountIndexViewModel
。这意味着必须将2个偏词强类型化为AccountIndexViewModel
:
_LoginPartial
_RegisterPartial
如果不是,则在渲染它们时需要传递正确的视图模型。
就_OpenIDSignInPartial
而言,您是通过_OpenIDSignInPartial
动作将其渲染的
return PartialView(\"_OpenIdSignInPartial\");
根据错误消息,看起来ѭ7是强类型为ѭ8:
@model AccountSignInViewModel
因此,请确保在返回部分视图时传递此模型的实例:
AccountSignInViewModel signInViewModel = ...
return PartialView(\"_OpenIdSignInPartial\",signInViewModel);