Reputation: 105
I have a PartialViewResult in my MVC. If I open this in Browser directly I have Data from my Model. If I open this in a other View I dont have Data. Can you tell me why?
Controller File:
public PartialViewResult PartialData() {
return PartialView(dataTable);
}
CSHTML File:
@model System.Data.DataTable
@using System.Data;
@this.Model.Columns[0]
call from another CSHTML
@Html.Partial("RunningJobs")
the Error on this line: @this.Model.Columns[0]
The object reference has not been set to an object instance.
Upvotes: 0
Views: 477
Reputation: 850
You will have to pass model to the parent view , when partial view is called in parent
public ActionResult ShowParentView() {
return view(dataTable);
}
Upvotes: 0
Reputation: 6783
When you are opening this from browser, means you are hitting the "PartialData" action where you are already passing model (i.e., dataTable). However, when you are loading the partial view from another CSHTML, you are not passing any model and it is null. Try using @Html.RenderAction from your main View instead of using @Html.Partial.
I think following should help you -
@Html.RenderAction("PartialData")
Upvotes: 2