简体   繁体   中英

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

I am trying to show some charts on my page using MVC Chart Helpers . For this is I have a view DisplaySales on which I have multiple partial views. One of my partial is _PlanwiseSales in which I want to draw a pie chart. 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. 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

 <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 Partial view

 [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. 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.

Please note I have tried writing Url.Action in these two forms

  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. You need send request like: /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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM