I created following custom ActionResult
which returns multiple partial views.
public class MultiplePartialViewsResult : ActionResult
{
private const string Separator = "-";
private PartialViewResult[] _partialViews;
public MultiplePartialViewsResult(params PartialViewResult[] partialViews)
{
_partialViews = partialViews;
}
public override void ExecuteResult(ControllerContext context)
{
foreach (var partialView in _partialViews)
{
partialView.ExecuteResult(context);
context.HttpContext.Response.Output.Write(Separator);
}
}
}
And then I use it as follows:
return new MultiplePartialViewsResult(
PartialView("~/Views/RowSumView.cshtml", new List<double>() { 1.0 }),
PartialView("~/Views/ColumnSumView.cshtml", new List<double>() { 2.0 }));
But if I place breakpoint in MultiplePartialViewsResult
's constructor I see that model in both cases equals 2.0
. It overrides all models with the last model specified in a array.
Try to create couple PartialViews in method and assign it to variable. Then you will notice they all share equivalent model.
You can initialize the MultiplePartialViewsResult object in the following manner with new own ViewData / ViewData.Model instances (works fine for me):
return new MultiplePartialViewsResult(
//PartialView("~/Views/RowSumView.cshtml", new List<double>() { 1.0 }),
//PartialView("~/Views/ColumnSumView.cshtml", new List<double>() { 2.0 })
new PartialViewResult() { ViewName = "~/Views/RowSumView.cshtml", ViewData = new ViewDataDictionary() { Model = new List<double>() { 1.0 } } },
new PartialViewResult() { ViewName = "~/Views/ColumnSumView.cshtml", ViewData = new ViewDataDictionary() { Model = new List<double>() { 2.0 } } }
);
Custom Action Result:
public class MultiplePartialViewsResult : ActionResult {
private const string Separator = "-";
private PartialViewResult[] _partialViews;
public MultiplePartialViewsResult(params PartialViewResult[] partialViews) {
_partialViews = partialViews;
}
public override void ExecuteResult(ControllerContext context) {
foreach(var partialView in _partialViews) {
partialView.ExecuteResult(context);
context.HttpContext.Response.Output.Write(Separator);
}
}
}
Controller:
public ActionResult Index() {
return View();
}
public ActionResult FakeAction() {
return new MultiplePartialViewsResult(
//PartialView("~/Views/RowSumView.cshtml", new List<double>() { 1.0 }),
//PartialView("~/Views/ColumnSumView.cshtml", new List<double>() { 2.0 })
new PartialViewResult() { ViewName = "~/Views/RowSumView.cshtml", ViewData = new ViewDataDictionary() { Model = new List<double>() { 1.0 } } },
new PartialViewResult() { ViewName = "~/Views/ColumnSumView.cshtml", ViewData = new ViewDataDictionary() { Model = new List<double>() { 2.0 } } }
);
}
Views:
Index:
@Html.Action("FakeAction")
ColumnSumView.cshtml / RowSumView.cshtml:
@model List<double>
<ul>
@foreach(double item in Model) {
<li>@item</li>
}
</ul>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With