模型项传入字典...... [英] The model item passed into the dictionary...

查看:69
本文介绍了模型项传入字典......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我遇到问题,当从AdminController调用方法(AdminList)时,遵循正确的代码路径(* 2)但之后它使用不同的模型(LoginModel)加载具有部分视图(* 3)的_Layout(* 1)。我收到以下错误 - > 传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [Ts.Global.Models.AdminModel]',但此字典需要类型为'Ts.Global.Models.TsAccount的模型项。 LoginModel'。



任何帮助都将受到高度赞赏。



1. _Layout.cshtml是关注

Hi,

I am having an issue, when a method(AdminList) is called from AdminController the correct code path is followed(*2) but thereafter it loads the _Layout(*1) which has a Partial View(*3) with a different Model(LoginModel). I get the following error -> "The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Ts.Global.Models.AdminModel]', but this dictionary requires a model item of type 'Ts.Global.Models.TsAccount.LoginModel'."

Any assistance would be highly appreciated.

1. _Layout.cshtml is as follows

<body>
    @Html.Partial("Sections/_Header") @*Model: LoginModel*@
    @RenderBody()    
    @Html.Partial("Sections/_Footer")
</body>



2. AdminController / AdminList


2. AdminController/AdminList

return View(adminModel);



3. AccountController /登录


3. AccountController/Login

return PartialView(webLoginModel);



4. Login.cshtml - 部分视图


4. Login.cshtml - Partial View

@using Ts.Global.Helpers;
@model Ts.Global.Models.TsAccount.LoginModel
<div id="mHeader" class="mHeader"> 
</div>



5. AdminList.cshtml


5. AdminList.cshtml

@model List<Ts.Global.Models.AdminModel>
<table>
    <tr>
        <td>Select Administrator</td>
        <td></td>
    </tr>  
</table>

推荐答案

错误很明显,View需要 AdminModel &你正在传递 LoginModel



你可以做的是,把这两个模型合二为一,然后通过一个模型只对视图。

例如,

Error is pretty clear, View requires the AdminModel & You're passing the LoginModel.

What you can do is, combine those two models in one and pass that one model only to the view.
For example,
public class UserViewModel
{
    public AdminModel adminModel;
    public LoginMode loginModel;
}





-KR



-KR


这篇关于模型项传入字典......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆