Home Two models in a view
Reply: 1

Two models in a view

manhart
1#
manhart Published in 2017-12-05 14:03:17Z

This question already has an answer here:

  • Multiple models in a view 9 answers

I have a view in cshtml and some model to view values in a columns. In a columns Name4 and Name5 I want to show values from second model and I don't know how to do it. For example now I showing values from model Project.Models.Names but in a column Name4 and Name5 I want to show values from model Project.Models.Surnames for example.

@model IEnumerable<Project.Models.Names>



@{
ViewBag.Title = "Smt";

}

<h2>Smt</h2>


<table class="table">
<tr>
    <th>
        Name
    </th>
    <th>
        Name1
    </th>
    <th>
        Name2
    </th>
    <th>
        Name3
    </th>
    <th>
        Name4
    </th>
    <th>
        Name5
    </th>
    <th>

    </th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Name1)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Name2)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Name3)
    </td>
    <td>
        ...value from second model
    </td>
    <td>
        ...value from second model
    </td>
Rich Bryant
2#
Rich Bryant Reply to 2017-12-05 14:49:37Z

Create a Viewmodel class that has properties of Name and Surname. Then bind to that.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.342258 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO