MVC在具有不同模型的多个视图中使用PartialView

时间:2018-11-25 09:55:59

标签: c# asp.net asp.net-mvc razor

这个问题已经得到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 });
}

1 个答案:

答案 0 :(得分:0)

你不能那样做。您可以在不同的主视图中使用具有相同模型的局部视图。实现此目的的逻辑方法是使用通用。但是我们不能在mvc部分视图中使用它