简体   繁体   English

在MVC应用程序中使用两个模型,一个在视图中,一个在局部视图中

[英]Using two models in MVC application, one in a view and one in a partial

I am having some trouble. 我遇到了麻烦。 I have a view as below 我有以下观点

@model IEnumerable<Tipstr.Models.Posts>

<div class ="mainC">
<div class = "leftContent">


  @Html.Partial("_LeaderboardPartial")

</div>
<div class = "rightContent">
@foreach (var item in Model) {

        <h1 class ="ptitle">
        @Html.DisplayFor(modelItem => item.title)
            </h1>
   <p class ="date">
       posted @Html.DisplayFor(modelItem => item.date)
    </p>
<p class ="post">
        @Html.DisplayFor(modelItem => item.body)
  </p>
<hr />
}
</div>
</div>
    <div class="Cpad">
        <br class="clear" /><div class="Cbottom"></div><div class="Cbottomright">                           
    </div>
    </div>

And I have a partial, _LeaderboardPartial, as shown 我有一个_LeaderboardPartial,如图所示

 @model IEnumerable<Tipstr.Models.Leaderboard>


<table id="pattern-style-a" summary="Meeting Results">
<thead>
    <tr>
        <th scope="col">Username</th>
        <th scope="col">Tipster Score</th>

    </tr>
</thead>
<tbody>
@foreach (var item in Model) {



    <tr>
        <td>@Html.DisplayFor(modelItem => item.userName)</td>
        <td> @Html.DisplayFor(modelItem => item.score)</td>

    </tr>

}
    </tbody>
 </table>

I can't seem to get it to work I think it has something to do with passing in one model from the layout and then another from the partial. 我似乎无法使其正常工作,我认为这与从布局传入一个模型,然后从局部传入另一个模型有关。 How can I get around this? 我该如何解决? I get the following error: 我收到以下错误:

The model item passed into the dictionary is of type 'System.Collections.Generic.List 1[Tipstr.Models.Posts]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1[Tipstr.Models.Leaderboard]'. 传递到词典中的模型项的类型为'System.Collections.Generic.List 1[Tipstr.Models.Posts]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable类型为'System.Collections.Generic.IEnumerable 1[Tipstr.Models.Posts]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 。[Leaderboard]]。

As @David Tansey said, you can have a view model that contains a reference to the two types 正如@David Tansey所说,您可以拥有一个包含对这两种类型的引用的视图模型

public class MyViewModel
{
    public MyViewModel(IEnumerable<Posts> posts, 
                       IEnumerable<Leaderboard> leaderboard)
    {
        //you can add null checks to ensure view model invariants

        this.Posts = posts;
        this.Leaderboard = leaderboard;
    }

    public IEnumerable<Posts> Posts { get; private set; }
    public IEnumerable<Leaderboard> Leaderboard{ get; private set; }
}

And in your main view you will call Html.Partial like this 在主视图中,您将这样调用Html.Partial

@model MyViewModel

<div class ="mainC">
<div class = "leftContent">


@Html.Partial("_LeaderboardPartial", this.Model.Leaderboard)

</div>
....

If you do this, you should change the foreach to iterate through Model.Posts instead of Model 如果执行此操作,则应更改foreach以通过Model.Posts而不是Model进行迭代

@foreach (var item in this.Model.Posts)

This line of code: 这行代码:

@Html.Partial("_LeaderboardPartial") in the first view implies sending along its own model IEnumerable<Tipstr.Models.Posts> to the partial. 在第一个视图中, @Html.Partial("_LeaderboardPartial")表示将其自身的模型IEnumerable<Tipstr.Models.Posts>发送到局部。

The partial expects a reference to type IEnumerable<Tipstr.Models.Leaderboard> . 该部分期望引用为IEnumerable<Tipstr.Models.Leaderboard>类型。

Perhaps you need a view model that contains a reference to one of each of these types? 也许您需要一个包含对这些类型之一的引用的视图模型?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM