c# - 如何传递正确的 Model Diction 类型 MVC

标签 c# dictionary model-view-controller asp.net-mvc-5 asp.net-mvc-partialview

我是 MVC 5 的新手,我创建了一个新项目并且正在设置登录(使用外部登录)。

这是使用 VIsual Studio 2013 MVC 5 应用程序模板

所以发生的事情是,当我点击社交媒体登录按钮时,我收到错误消息,提示传递了错误的模型

The model item passed into the dictionary is of type 'WebPortal.Models.LoginViewModel', but this dictionary requires a model item of type 'WebPortal.Models.ExternalLoginListViewModel'.

如果您需要控件和模型代码,请告诉我。但正如我所说,这是模板附带的默认代码。此时我一直在更改的唯一内容是更改外观的 View 。并在下方发布了查看代码。

我认为问题是因为我是从布局页面开始的,所以模型永远不会启动,因为没有布局模型......我还是新手,所以我只是猜测。

这是部分路径 "_Layout"-> "_SocialBar"(局部 View ) -> "Login"(局部 View ) -> "LoginPartial"(局部 View ) -> "_ExternalLoginsList"(局部 View )

SocialBar(局部 View )

<div class="header-top dark ">
    <div class="container">
        <div class="row">
            <div class="col-xs-3 col-sm-6 col-md-9">
                ...Some Code....
                <!-- header-top-first end -->
            </div>
            <div class="col-xs-9 col-sm-6 col-md-3">
                @Html.Partial("_Login")
            </div>
        </div>
    </div>
</div>

登录局部 View 页面

@using Microsoft.AspNet.Identity

@if (Request.IsAuthenticated)
{
    ...Some Code...
}
else
{
    ...Some Code...
    @Html.Partial("_LoginPartial")
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

登录部分查看代码

@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>
}

外部登录列表代码

@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&nbsp&nbsp&nbsp

                @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>
    }
}

最佳答案

当您执行 @Html.Partial() 并且不为其提供模型时,它会使用当前(或父级,如果您想那样想的话)模型。所以当你这样做的时候

@Html.Partial("../Account/_ExternalLoginsList")

如果没有第二个参数指定型号,则提供当前型号。您应该像这样为部分提供 View 模型:

@Html.Partial("../Account/_ExternalLoginsList", externalLoginsViewModel)

否则,您将提供当前类型,这与部分所期望的类型不同。

关于c# - 如何传递正确的 Model Diction 类型 MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31274579/

相关文章:

asp.net-mvc - MVC 局部 View 不呈现

c# - 如何读取 MVC Controller 方法中的 web.config 设置并在 angularjs View 、 Controller 和服务中访问它们?

java - 使用 JFace 的查看器是否会妨碍良好的 MVC 分离?

c# - 在哪里放置业务实体、枚举、自定义异常?

c# - 并非所有代码路径都返回值

c# - 协助使用 rhino 模拟为方法编写单元测试

c# - 返回类型为 Task<Dictionary<string, string>> 的任务

c# - 接口(interface)问题

python - 如何将多个值添加到 Python 字典中的键

python - 有效使用Python map