如何在一个视图中放置两个模型

时间:2016-12-19 08:29:20

标签: c# asp.net-mvc

如何在一个视图中放置两个模型。我只是新的,这就是为什么我不能理解这个问题的其他答案。请帮帮我。非常感谢您的理解。我需要及时完成它。

查看

@model IEnumerable, PagedList.IPagedLis<ARM2.Models.Institution>
@model PagedList.IPagedList<ARM2.Models.Institution>, 
@using PagedList.Mvc;

<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
ViewBag.Title = "Insitution";
Layout = "~/Views/Shared/_Layout.cshtml";
}
}

<div class="container-fluid" id="page-content-wrapper">
<h2>INSTITUTION MANAGEMENT</h2>
<br />

<div class="panel panel-default">
    <div class="panel-heading">List of Institutions</div>
    <div class="panel-body">
        <button type="button" class="btn btn-primary pull-right btn-sm"        data-toggle="modal" data-  target="#addInstitutionModal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add&nbsp;&nbsp;&nbsp     ;&nbsp;&nbsp;</button>

        <br /><br />
        <p>
            @Html.ActionLink("Create New", "Create")  
        </p>

 @using (Html.BeginForm("Index", "Insitution", FormMethod.Get))
 {
    <p>
    Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as   string)
    <input type="submit" value="Search" />
    </p>
  }
        <table class="table">
            <tr>
                <th>
                   @Html.DisplayNameFor(model => model.InstitutionID)
                </th>
                <th>
                    @Html.ActionLink("Institution Name", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.IIN)
                </th>
                <th>
                    @Html.ActionLink("Date Added", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter })
                </th>
                <th>
                    Action
                </th>
                <th></th>
            </tr>

            @foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.InstitutionID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.InstitutionName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.IIN)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DateAdded)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.InstitutionID }) |

            @Html.ActionLink("Details", "Details", new { id=item.InstitutionID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.InstitutionID })
        </td>
    </tr>
}

        </table>
        <br />
        Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of    @Model.PageCount

        @Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

    </div>
 </div>

CONTROLLER

    namespace ARM2.Controllers
   {
   public class InstitutionsController : Controller
    {
    private InstitutionDBContext db = new InstitutionDBContext();

    // GET: Institutions
    public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
    {
        ViewBag.CurrentSort = sortOrder;
        ViewBag.IDParm = String.IsNullOrEmpty(sortOrder) ? "ID_desc" : "";
        ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
        ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

        if (searchString != null)
        {
            page = 1;
        }
        else
        {
            searchString = currentFilter;
        }

        ViewBag.CurrentFilter = searchString;
        var insti = from s in db.institutions
                    select s;
        if (!String.IsNullOrEmpty(searchString))
        {
            insti = insti.Where(s => s.InstitutionName.Contains(searchString));

        }

        switch (sortOrder)
        {
            case "ID_desc":
                insti = insti.OrderByDescending(s => s.InstitutionID);
                break;
            case "name_desc":
                insti = insti.OrderByDescending(s => s.InstitutionName);
                break;
            case "Date":
                 insti = insti.OrderBy(s => s.DateAdded);
                break;
            case "date_desc":
                insti = insti.OrderByDescending(s => s.DateAdded);
                break;
            default:
                insti = insti.OrderBy(s => s.InstitutionID);
                break;

        }
        int pageSize = 10;
        int pageNumber = (page ?? 1);
        return View(insti.ToPagedList(pageNumber, pageSize));

    }

MODEL

  namespace ARM2.Models
{
public class Institution
{
    public int InstitutionID { get; set; }

    public string InstitutionName { get; set; }

    public int IIN { get; set; }

    public DateTime DateAdded { get; set; }


}

public class InstitutionDBContext : DbContext
{

    public DbSet<Institution> institutions { get; set; }
}
 } 

1 个答案:

答案 0 :(得分:3)

这是不可能的。您应该创建一个包含这两个模型的ViewModel。