在局部视图中将 List<ComplexObject> 传递给 Url.Action
Passing List<ComplexObject> to Url.Action inside a partial view
我正在尝试使用 MVC Chart Helpers
在我的页面上显示一些图表。为此,我有一个视图 DisplaySales
,在该视图上我有多个局部视图。我的部分内容之一是 _PlanwiseSales
,我想在其中绘制一个饼图。
在加载 DisplaySales
视图时,我正在查询数据库并获取多个表中的所有必需数据,并将其映射到相应的 类。下面是我的 类 和填充这些的代码:
public class DashboardView
{
[JsonProperty("Table")]
public List<CounterView> Counters { get; set; }
[JsonProperty("Table1")]
public List<PlanwiseSalesView> PlanwiseSalesView { get; set; }
[JsonProperty("Table2")]
public List<DaywiseSalesView> DaywiseSalesView { get; set; }
[JsonProperty("Table3")]
public List<StorewiseSalesView> StorewiseSalesView { get; set; }
[JsonProperty("Table4")]
public List<ProductwiseSalesView> ProductwiseSalesView { get; set; }
[JsonProperty("Table5")]
public List<PartnerSalesView> SalesView { get; set; }
}
public class PlanwiseSalesView
{
public string PlanTypeName { get; set; }
public int Quantity { get; set; }
}
/// <summary>
/// Shows Sales screen
/// </summary>
/// <returns> </returns>
[Route("store-sales/{id}/{startDate?}/{endDate?}")]
public async Task<ActionResult> DisplaySales(string id , DateTime? startDate, DateTime? endDate)
{
var dashboard= await new PartnerServices().GetPartnerSalesFigures(id, startDate, endDate );
var partners = await new PartnerServices().GetAllChannelPartnersOfAStore(id);
ViewBag.Partners = partners;
ViewBag.Header = $"Sales";
return View(dashboard);
}
使用这部分代码呈现分部视图并将列表传递给 DisplaySales 视图上的分部视图
<div class="col-md-3">
@{
Html.RenderPartial("~/Views/Shared/_PlanwiseSales.cshtml", Model.PlanwiseSalesView);
}
</div>
_Planwisesales.cshtml
@using GoWarranty.Models;
@model List<PlanwiseSalesView>
<div class="card">
<div class="card-body">
<div class="d-md-flex align-items-center">
<div>
<h4 class="card-title">Plan wise Sales of <span class="badge badge-pill badge-purple font-bold text-uppercase">@string.Format("{0}", ViewBag.StoreName)</span></h4>
</div>
</div>
<div class="row">
<img src="@Url.Action("ShowPlanwiseSales", "Sales", new { chartdata =Model})" alt="@string.Format("Plan wise Sales of {0}", ViewBag.StoreName)" />
</div>
</div>
</div>
C# - Planwisesales 局部视图
[HttpGet]
public ActionResult ShowPlanwiseSales(List<PlanwiseSalesView> chartdata)
{
var chart = new Chart(width: 400, height: 400)
.AddSeries(chartType: "pie",
xValue: chartdata, xField: "PlanTypeName",
yValues: chartdata, yFields: "Quantity")
.AddTitle("Plan wise sales")
.GetBytes("png");
return File(chart, "image/bytes");
}
问题
我的问题是我在 ShowPlanwiseSales 操作方法的参数图表数据中得到计数 ==0。预期结果将是从父视图传递的项目数。在我的例子中,我从父视图的部分视图模型中获取了 2 个项目。
请注意我试过用这两种形式写Url.Action
Url.Action("ShowPlanwiseSales", "Sales", new { chartdata =Json.Encode( Model)});
Url.Action("ShowPlanwiseSales", "Sales", new { chartdata = Html.Raw(Json.Encode( Model))});
您的代码将生成无法反序列化的 url,如:/Sales/ShowPlanwiseSales?chartdata=[{"PlanTypeName":"xx","Quantity":xx},{"PlanTypeName":"xx","Quantity":xx}]
。您需要发送这样的请求:/Sales/ShowPlanwiseSales?[0].PlanTypeName=xx&[0].Quantity=xx&[1].PlanTypeName=xx&[1].Quantity=xx
.
更改以下代码:
@{
var qs = HttpUtility.ParseQueryString("");
for(int i =0;i<Model.Count();i++)
{
for(int j =0;j< Model[i].GetType().GetProperties().Length;j++)
{
var name = Model[i].GetType().GetProperties()[j].Name;
var keyname = "[" + i + "]." + name;
var value = Model[i].GetType().GetProperty(name).GetValue(Model[i], null).ToString();
qs.Add(keyname, value);
}
}
}
<img src="@Url.Action("ShowPlanwiseSales", "Sales" )?@qs" />
我正在尝试使用 MVC Chart Helpers
在我的页面上显示一些图表。为此,我有一个视图 DisplaySales
,在该视图上我有多个局部视图。我的部分内容之一是 _PlanwiseSales
,我想在其中绘制一个饼图。
在加载 DisplaySales
视图时,我正在查询数据库并获取多个表中的所有必需数据,并将其映射到相应的 类。下面是我的 类 和填充这些的代码:
public class DashboardView
{
[JsonProperty("Table")]
public List<CounterView> Counters { get; set; }
[JsonProperty("Table1")]
public List<PlanwiseSalesView> PlanwiseSalesView { get; set; }
[JsonProperty("Table2")]
public List<DaywiseSalesView> DaywiseSalesView { get; set; }
[JsonProperty("Table3")]
public List<StorewiseSalesView> StorewiseSalesView { get; set; }
[JsonProperty("Table4")]
public List<ProductwiseSalesView> ProductwiseSalesView { get; set; }
[JsonProperty("Table5")]
public List<PartnerSalesView> SalesView { get; set; }
}
public class PlanwiseSalesView
{
public string PlanTypeName { get; set; }
public int Quantity { get; set; }
}
/// <summary>
/// Shows Sales screen
/// </summary>
/// <returns> </returns>
[Route("store-sales/{id}/{startDate?}/{endDate?}")]
public async Task<ActionResult> DisplaySales(string id , DateTime? startDate, DateTime? endDate)
{
var dashboard= await new PartnerServices().GetPartnerSalesFigures(id, startDate, endDate );
var partners = await new PartnerServices().GetAllChannelPartnersOfAStore(id);
ViewBag.Partners = partners;
ViewBag.Header = $"Sales";
return View(dashboard);
}
使用这部分代码呈现分部视图并将列表传递给 DisplaySales 视图上的分部视图
<div class="col-md-3">
@{
Html.RenderPartial("~/Views/Shared/_PlanwiseSales.cshtml", Model.PlanwiseSalesView);
}
</div>
_Planwisesales.cshtml
@using GoWarranty.Models;
@model List<PlanwiseSalesView>
<div class="card">
<div class="card-body">
<div class="d-md-flex align-items-center">
<div>
<h4 class="card-title">Plan wise Sales of <span class="badge badge-pill badge-purple font-bold text-uppercase">@string.Format("{0}", ViewBag.StoreName)</span></h4>
</div>
</div>
<div class="row">
<img src="@Url.Action("ShowPlanwiseSales", "Sales", new { chartdata =Model})" alt="@string.Format("Plan wise Sales of {0}", ViewBag.StoreName)" />
</div>
</div>
</div>
C# - Planwisesales 局部视图
[HttpGet]
public ActionResult ShowPlanwiseSales(List<PlanwiseSalesView> chartdata)
{
var chart = new Chart(width: 400, height: 400)
.AddSeries(chartType: "pie",
xValue: chartdata, xField: "PlanTypeName",
yValues: chartdata, yFields: "Quantity")
.AddTitle("Plan wise sales")
.GetBytes("png");
return File(chart, "image/bytes");
}
问题
我的问题是我在 ShowPlanwiseSales 操作方法的参数图表数据中得到计数 ==0。预期结果将是从父视图传递的项目数。在我的例子中,我从父视图的部分视图模型中获取了 2 个项目。
请注意我试过用这两种形式写Url.Action
Url.Action("ShowPlanwiseSales", "Sales", new { chartdata =Json.Encode( Model)});
Url.Action("ShowPlanwiseSales", "Sales", new { chartdata = Html.Raw(Json.Encode( Model))});
您的代码将生成无法反序列化的 url,如:/Sales/ShowPlanwiseSales?chartdata=[{"PlanTypeName":"xx","Quantity":xx},{"PlanTypeName":"xx","Quantity":xx}]
。您需要发送这样的请求:/Sales/ShowPlanwiseSales?[0].PlanTypeName=xx&[0].Quantity=xx&[1].PlanTypeName=xx&[1].Quantity=xx
.
更改以下代码:
@{
var qs = HttpUtility.ParseQueryString("");
for(int i =0;i<Model.Count();i++)
{
for(int j =0;j< Model[i].GetType().GetProperties().Length;j++)
{
var name = Model[i].GetType().GetProperties()[j].Name;
var keyname = "[" + i + "]." + name;
var value = Model[i].GetType().GetProperty(name).GetValue(Model[i], null).ToString();
qs.Add(keyname, value);
}
}
}
<img src="@Url.Action("ShowPlanwiseSales", "Sales" )?@qs" />