C# 如何通过正确的模型定义类型MVC
我是MVC5新手,我已经创建了一个新项目,我正在设置登录(使用外部登录) 这是使用VIsual Studio 2013 MVC 5应用程序模板 因此,当我点击社交媒体登录的按钮时,我得到了一个错误,即传递了错误的模型 传递到字典中的模型项的类型为 “WebPortal.Models.LoginViewModel”,但此词典需要 类型为“WebPortal.Models.ExternalLoginListViewModel”的模型项 如果您需要控件和模型代码,请告诉我。但正如我所说,这是模板附带的默认代码。我唯一一直在改变的是在这一点上的看法,以改变外观。并在下面发布了查看代码 我认为问题在于,因为我是从布局页面开始的,所以模型永远不会被启动,因为没有布局的模型。。。。我又是新来的,所以我只是在猜测 这是部分路径 “\u布局”->”\u社交网站”(部分视图)->“登录”(部分视图)->“登录”(部分视图)->“登录”(部分视图)->“外部登录列表”(部分视图) SocialBar(局部视图) 逻辑分区视图代码C# 如何通过正确的模型定义类型MVC,c#,dictionary,model-view-controller,asp.net-mvc-5,asp.net-mvc-partialview,C#,Dictionary,Model View Controller,Asp.net Mvc 5,Asp.net Mvc Partialview,我是MVC5新手,我已经创建了一个新项目,我正在设置登录(使用外部登录) 这是使用VIsual Studio 2013 MVC 5应用程序模板 因此,当我点击社交媒体登录的按钮时,我得到了一个错误,即传递了错误的模型 传递到字典中的模型项的类型为 “WebPortal.Models.LoginViewModel”,但此词典需要 类型为“WebPortal.Models.ExternalLoginListViewModel”的模型项 如果您需要控件和模型代码,请告诉我。但正如我所说,这是模板附带的
@using WebPortal.Models
@model LoginViewModel
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { role = "form" }))
{
@Html.AntiForgeryToken()
<form class="login-form margin-clear">
<div class="form-group has-feedback">
<label class="control-label">@Html.LabelFor(m => m.Email)</label>
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
<i class="fa fa-user form-control-feedback"></i>
</div>
<div class="form-group has-feedback">
<label class="control-label">@Html.LabelFor(m => m.Password)</label>
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
<i class="fa fa-lock form-control-feedback"></i>
</div>
<div class="form-group has-feedback">
@Html.CheckBoxFor(m => m.RememberMe)
<label class="control-label">@Html.LabelFor(m => m.RememberMe)</label>
</div>
<input type="submit" value="Log in" class="btn btn-gray btn-sm" />
<span class="pl-5 pr-5">or</span>
@Html.ActionLink("Sign Up", "Register", "Account", null, new { @class = "btn btn-default btn-sm" })
<div class="form-group has-feedback">
<a href="#">Forgot your password?</a>
</div>
@Html.Partial("../Account/_ExternalLoginsList")
</form>
}
@使用WebPortal.Models
@模型LoginView模型
@使用(Html.BeginForm(“Login”,“Account”,new{ReturnUrl=ViewBag.ReturnUrl},FormMethod.Post,new{role=“form”}))
{
@Html.AntiForgeryToken()
@LabelFor(m=>m.Email)
@TextBoxFor(m=>m.Email,新的{@class=“form control”})
@LabelFor(m=>m.Password)
@Html.PasswordFor(m=>m.Password,新的{@class=“form control”})
@CheckBoxFor(m=>m.RememberMe)
@LabelFor(m=>m.RememberMe)
或
@ActionLink(“注册”、“注册”、“帐户”、空、新{@class=“btn btn default btn sm”})
@Html.Partial(“../Account/\u ExternalLoginsList”)
}
外部登录列表代码
@model WebPortal.Models.ExternalLoginListViewModel
@using Microsoft.Owin.Security
@{
var loginProviders = Context.GetOwinContext().Authentication.GetExternalAuthenticationTypes();
if (loginProviders.Count() == 0)
{
<span class="text-center">No External Logins</span>
}
else
{
<span>Login with   
@foreach (AuthenticationDescription p in loginProviders)
{
switch (@p.AuthenticationType)
{
case "Facebook":
<button type="submit" class="btn btn-xsm facebook" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType"><i class="fa fa-facebook"></i></button>
break;
case "Twitter":
<button type="submit" class="btn btn-xsm twitter" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType"><i class="fa fa-twitter"></i></button>
break;
case "Google":
<button type="submit" class="btn btn-xsm googleplus" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType"><i class="fa fa-google-plus"></i></button>
break;
case "Microsoft":
<button type="submit" class="btn btn-xsm microsoft" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType"><i class="fa fa-windows"></i></button>
break;
default:
<button type="submit" class="btn btn-xsm" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType">@p.AuthenticationType.ToString()</button>
break;
}
}
</span>
}
}
@model WebPortal.Models.ExternalLoginListViewModel
@使用Microsoft.Owin.Security
@{
var loginProviders=Context.GetOwinContext().Authentication.GetExternalAuthenticationTypes();
if(loginProviders.Count()==0)
{
没有外部登录
}
其他的
{
使用登录
@foreach(loginProviders中的AuthenticationDescription p)
{
开关(@p.AuthenticationType)
{
案例“Facebook”:
打破
案例“推特”:
打破
案例“谷歌”:
打破
案例“微软”:
打破
违约:
@p、 AuthenticationType.ToString()
打破
}
}
}
}
当您执行@Html.Partial()
并且不为其提供模型时,它将使用当前(或者父级,如果您想这样想的话)模型。所以当你这么做的时候
@Html.Partial("../Account/_ExternalLoginsList")
如果没有第二个参数来指定模型,它将提供当前参数。您应该向零件提供视图模型,如下所示:
@Html.Partial("../Account/_ExternalLoginsList", externalLoginsViewModel)
否则,您将提供当前类型,该类型与partial所期望的类型不同。ExternalLoginList partial期望向其传递一个模型
@model WebPortal.Models.ExternalLoginListViewModel
@Html.Partial("../Account/_ExternalLoginsList", externalLoginListViewModel)
@model WebPortal.Models.ExternalLoginListViewModel
您需要在此处传递模型:
@Html.Partial("../Account/_ExternalLoginsList")
像这样:
@Html.Partial("../Account/_ExternalLoginsList", new ExternalLoginListViewModel())
或
将分部的模型放入父模型中
public class LoginViewModel
{
public ExternalLoginListViewModel externalLoginListViewModel;
}
并将其传递给分部
@Html.Partial("../Account/_ExternalLoginsList", externalLoginListViewModel)
或
我在ExternalLoginList部分中没有看到任何对模型的引用,因此您可能只需删除这一行:
@model WebPortal.Models.ExternalLoginListViewModel