简体   繁体   中英

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

 @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]'.

As @David Tansey said, you can have a view model that contains a reference to the two types

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

@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 (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.

The partial expects a reference to type IEnumerable<Tipstr.Models.Leaderboard> .

Perhaps you need a view model that contains a reference to one of each of these types?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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