简体   繁体   English

通过名单<complexobject>局部视图内的 Url.Action</complexobject>

[英]Passing List<ComplexObject> to Url.Action inside a partial view

I am trying to show some charts on my page using MVC Chart Helpers .我正在尝试使用MVC Chart Helpers在我的页面上显示一些图表。 For this is I have a view DisplaySales on which I have multiple partial views.为此,我有一个视图DisplaySales ,在该视图上我有多个部分视图。 One of my partial is _PlanwiseSales in which I want to draw a pie chart.我的一个部分是_PlanwiseSales ,我想在其中绘制一个饼图。 On the load of the DisplaySales view, I am querying the database and fetching all required data in multiple tables, along with mapping it with respective classes.在加载DisplaySales视图时,我正在查询数据库并在多个表中获取所有必需的数据,并将其映射到相应的类。 Below are my classes and code to fill those:下面是我的类和代码来填充这些:

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);
 }

Using this fraction of code for rendering partial view along with passing the list to the partial view on DisplaySales view使用这部分代码呈现局部视图,同时将列表传递给 DisplaySales 视图上的局部视图

 <div class="col-md-3">
        @{
            Html.RenderPartial("~/Views/Shared/_PlanwiseSales.cshtml", Model.PlanwiseSalesView);
        }
    </div>

_Planwisesales.cshtml _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 Partial view 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");
        }

Problem问题

My problem is that I am getting count ==0 in the parameter chartdata of the ShowPlanwiseSales action method.我的问题是我在 ShowPlanwiseSales 操作方法的参数 chartdata 中得到 count ==0 。 The expected result will the number of items passed from the parent view.预期结果将是从父视图传递的项目数。 here in my case, I am getting 2 items in my partial view model from the parent view.在我的情况下,我从父视图的部分视图 model 中获得 2 个项目。

Please note I have tried writing Url.Action in these two forms请注意我已经尝试在这两个 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))});

Your code will generate the url like: /Sales/ShowPlanwiseSales?chartdata=[{"PlanTypeName":"xx","Quantity":xx},{"PlanTypeName":"xx","Quantity":xx}] which cannot be deserialized.您的代码将生成 url ,例如: /Sales/ShowPlanwiseSales?chartdata=[{"PlanTypeName":"xx","Quantity":xx},{"PlanTypeName":"xx","Quantity":xx}]被反序列化。 You need send request like: /Sales/ShowPlanwiseSales?[0].PlanTypeName=xx&[0].Quantity=xx&[1].PlanTypeName=xx&[1].Quantity=xx .您需要发送请求,例如: /Sales/ShowPlanwiseSales?[0].PlanTypeName=xx&[0].Quantity=xx&[1].PlanTypeName=xx&[1].Quantity=xx

Change your code below:在下面更改您的代码:

@{
    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