1

my controller is

    public ActionResult Index()
    {

        if (Request.IsAjaxRequest())
        {
            List<double> nyValues = new List<double> { 83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3 };
            List<ColumnSeriesData> nyData = new List<ColumnSeriesData>();
            nyValues.ForEach(p => nyData.Add(new ColumnSeriesData { Y = p }));
            ViewData["tokyoData"] = nyData;

            return PartialView("~/Views/Home/_Graph.cshtml");
        }
        else

        {
            List<double> tokyoValues = new List<double> { 49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4 };
            List<ColumnSeriesData> tokyoData = new List<ColumnSeriesData>();
            tokyoValues.ForEach(p => tokyoData.Add(new ColumnSeriesData { Y = p }));
            ViewData["tokyoData"] = tokyoData;

            return View();
        }
    }

and In View

<div class="graphDiv" id="MainGraph">
@{ Html.RenderPartial("_Graph");}
</div>

and ajax call is

 @Ajax.ActionLink("Team", "Index", "Home", null, new AjaxOptions { HttpMethod = "GET", LoadingElementId = "divLoading", UpdateTargetId = "body", InsertionMode = InsertionMode.Replace, OnSuccess = "" }, new { })

The Partial view is not being rendered in the div.. Its just an empty page ...Any help ...Thanks in advance please.

Khan
  • 109
  • 2
  • 12
  • Is the AJAX Call successful? If not, what error are you getting? Try debugging: https://stackoverflow.com/questions/1820927/request-monitoring-in-chrome?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Mikaal Anwar May 19 '18 at 04:57
  • Thanks for ur comments. Ajax call is successfull. If I put a div before and after the graph Even that appeas in the page but graph doesnt appear.
    This dive dissappeared after the Ajax call
    – Khan May 19 '18 at 08:16
  • If the added `
    ` shows up in the view but the graph is missing, it means that Ajax / View Rendering part is fine, perhaps there's some problem with initializing the chart itself. I suggest you go through this tutorial in detail ... https://www.c-sharpcorner.com/article/dotnet-highcharts-with-asp-net-mvc/
    – Mikaal Anwar May 19 '18 at 19:28
  • Thanks Mikaal foryour suggestion. I found the mistake that I am initalizing the high charts both in main view and in partial view again and it gives error that High Charts have been initialized already. COuld you please guide me how to initialize them only once @using Highsoft.Web.Mvc.Charts { @(Html.Highsoft().Highcharts( new Highcharts {} ),"container1") – Khan May 24 '18 at 07:29
  • Sure thing ... can you post the code for the @{ Html.RenderPartial("_Graph");} partial view? – Mikaal Anwar May 25 '18 at 00:32
  • Please check out the answer below. – Mikaal Anwar May 25 '18 at 02:39

2 Answers2

1

the problem at our project was, that highcharts triggers two events:

  • DOMContentLoaded
  • readystatechange

https://developer.mozilla.org/en-US/docs/Web/API/Document/readystatechange_event

Highcharts is generating a method which is not fired by this eventlisteners. We have to run the "CreateChart"-Function, when Ajax succeeded.

if (document.addEventListener) 
{
  document.addEventListener("DOMContentLoaded", function() {
    createChartTargetChart(); //CreateChartFunction !!
  });
}
else if (document.attachEvent) 
{
  document.attachEvent("onreadystatechange", function() {
      if (document.readyState === "complete")
      {
        document.detachEvent("onreadystatechange", arguments.callee);
        createChartTargetChart(); //CreateChartFunction !!
      }
   });
}

Highsoft generates the function name automatic, in c# you can code:

string divId = "xxx"; // = the containers target Id 
var functionName = $"createChart{divId}()";
tom nobleman
  • 366
  • 3
  • 8
0

I think you're trying to render a chart in a partial view and render that partial view inside the main view. In order to properly render a "PartialView" inside a "MainView" via an AJAX request, please try the following:

MainView:

@{ ViewBag.Title = "MainView"; }

<script src="~/Scripts/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>

<h2>MainView</h2>
<div class="graphDiv" id="MainGraph">
  @Ajax.ActionLink("Team", "GetChart", "Home", new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "TargetForChart", InsertionMode = InsertionMode.Replace, OnSuccess = "" })
  <h2>The chart appears here:</h2>
  <div id="TargetForChart">

  </div>
</div>

PartialView:

<!-- Your chart html and js goes here -->

Controller:

public class HomeController : Controller
{
    public ActionResult MainView()
    {
        return View();
    }

    public PartialViewResult GetChart()
    {
        List<double> nyValues = new List<double> { 83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3 };
        List<ColumnSeriesData> nyData = new List<ColumnSeriesData>();
        nyValues.ForEach(p => nyData.Add(new ColumnSeriesData { Y = p }));
        ViewData["tokyoData"] = nyData;
        return PartialView("_Graph");
    }
}

And as long as your partial view contains the right logic, there wouldn't be any reason for it to not render. If you have duplicated the initialization logic for the chart on both the MainView and the PartialView, I would suggest you to remove it from the MainView and only keep it on the PartialView where it would actually be rendered. Do keep an eye on the console to spot and resolve any and all js errors.

Mikaal Anwar
  • 1,720
  • 10
  • 21
  • Hi @Mikaal Anwar thanks for your reply. I did exactly the same thing as you suggested but still no luck. ! I got
    and inside this as you suggested I am using the references for high charts and then I get
    and this div is empty then I get
    – Khan Jun 04 '18 at 08:10