简体   繁体   中英

MVC Use a PartialView in multiple views with different models

This question has been answered MVC PartialView in multiple Views with different models but I am not getting the syntax right. I would like to get the Model to a ViewData in controller and pass it to a view in code below.

_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>

Controller:

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 });
}

You can't do like that. You can use partial view with same model in different main views. The logical way to implement this is by using Generic. But we can't use it in mvc partial view

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