【发布时间】:2018-11-26 09:27:01
【问题描述】:
这个问题已经回答了MVC PartialView in multiple Views with different models,但我的语法不正确。我想将模型获取到控制器中的 ViewData 并将其传递给下面代码中的视图。
_PartialIndex.cshtml:
@*@model Models.Org.OrgListViewModel*@
@using PagedList.Mvc;
@{
ViewBag.Title = "Organization";
}
@section styles {
}
<div class="col-sm-11">
@if (Model.OrganizationList == null || Model.OrganizationList.Count == 0)
{
<div class="panel-body">
<h6 class="text-bold text-danger text-center"> No records</h6>
</div>
}
<div class="panel panel-default card-view">
@if (Model.OrganizationList.TotalItemCount > 0)
{
//Bind data to datatable
}
else
{
<h6 class="text-center text-danger">No Data Retrieved!</h6>
}
</div>
</div>
Index.cshtml:
@using Models.Leads
@model LeadsListViewModel
@using PagedList.Mvc;
@{
ViewBag.Title = "Leads";
}
@section styles {
enter code here
}
<div class="row">
@Html.Partial("_PartialIndex",ViewBag.Model) //ViewBag.Model Not working
</div>
控制器:
public async Task<ActionResult> Index(string searchText, int page = 1)
{
var model = await repo.GetLeadsAsync(searchText, page);
// ViewData["Model"] = "LeadsListViewModel"; //Cant get the syntax right
return View(new LeadsListViewModel() { LeadsList = model, TotalItemCount = model.TotalItemCount });
}
【问题讨论】:
-
使它们继承自同一个父模型类,然后在运行时进行一些向下转换。基本面向对象
标签: c# asp.net asp.net-mvc razor