我正在嘗試在我的頁面上使用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 操作方法的引數 chartdata 中得到 count ==0 。預期結果將是從父視圖傳遞的專案數。就我而言,我從父視圖中獲得了部分視圖模型中的 2 個專案。
請注意,我嘗試以這兩種形式撰寫 Url.Action
Url.Action("ShowPlanwiseSales", "Sales", new { chartdata =Json.Encode( Model)});Url.Action("ShowPlanwiseSales", "Sales", new { chartdata = Html.Raw(Json.Encode( Model))});
uj5u.com熱心網友回復:
您的代碼將生成/Sales/ShowPlanwiseSales?chartdata=[{"PlanTypeName":"xx","Quantity":xx},{"PlanTypeName":"xx","Quantity":xx}]無法反序列化的 url,例如:您需要發送請求,例如:/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" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/407663.html
標籤:
