簡體   English   中英

通過名單<complexobject>局部視圖內的 Url.Action</complexobject>

[英]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 操作方法的參數 chartdata 中得到 count ==0 。 預期結果將是從父視圖傳遞的項目數。 在我的情況下,我從父視圖的部分視圖 model 中獲得 2 個項目。

請注意我已經嘗試在這兩個 forms 中編寫 Url.Action

  1. Url.Action("ShowPlanwiseSales", "Sales", new { chartdata =Json.Encode( Model)});
  2. 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM