[英]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>
@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>
[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 操作方法的参数 chartdata 中得到 count ==0 。 预期结果将是从父视图传递的项目数。 在我的情况下,我从父视图的部分视图 model 中获得 2 个项目。
请注意我已经尝试在这两个 forms 中编写 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" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.