0

I have a strongly typed view to a CalculateModel where a user puts some information and make a ajax post to the controller, the controller executes some maths in this data and return a PartialView strongly typed to the ResultCalculateModel.

In the Result partial view, i have a d3 chart which is dynamically generated with some parameters that i have in the ResultCalculateModel. Heres some code:

 @model DTO.CalculateModel    
 //the html helpers here to user input some data 

   <div id='divOutPutData'> </div>

 <script>
 function getResult() {

        $.post("/GetResult", $('#form01').serialize())
            .success(function (result) {
                $('#divInputData').attr('style', 'display:none');
                $('#divOutPutData').append(result);
        };

   function drawChart(s,p,c){
     //code
   };
    </script>

The action:

 public ActionResult GetResult(CalculateModel model)
 {
        ResultCalculateModel result = _calculateResult.Calculate(model);
        return PartialView("Result", result);
 }

The result Partial View:

  @model DTO.ResultCalculateModel //the parameters of the drawChart function are in this model.
  //some Razor Helpers which is working
   <div id="chartResult"> </div> //i need to display the chart here

I would like to know how i can execute the drawChart function in partial view rendering?

7
  • what you mean with "strongly typed" ? Commented Feb 17, 2016 at 18:22
  • When did you call drawChart function? Commented Feb 17, 2016 at 18:25
  • @CodeNotFound yes, exactly what i need to know. Commented Feb 17, 2016 at 18:26
  • where do yo get the param values or drawChart method ? Commented Feb 17, 2016 at 18:27
  • @Shyju on the result variable. The partial view is strongly typed to a model. Commented Feb 17, 2016 at 18:28

3 Answers 3

1

This will help i think

//example function which posts data gets result.. puts html on dom, then calls DrawChart function
function PostStuff() {
    $.post("/GetResult", $('#form01').serialize()).success(function (result) {
        $('#divInputData').attr('style', 'display:none');
        $('#divOutPutData').append(result);
        //call function to interact with the data you just injected into the dom
        //get values from the partial view I made these up... you need to adjust for your situation.
        var s = $("#sId");
        var p = $("#pId");
        var c = $("#cId");
        DrawChart(s,p,c);       
    };
}

//example function
function DrawChart(s,p,c){
   //code
};
Sign up to request clarification or add additional context in comments.

5 Comments

yes but, how about the parameters of the drawChart? The result is an html.
could you explain what you mean...maybe showing more code will help. I guessing you just need to mark them global... but hard to say without seeing the code.
i have edited the post, showing the models on the views.
you still not showing the part i need to see. I modified answer to maybe be what you want?
this is promising, actually i can put these results in a hidden field.
1

Perhaps in the partial view .cshtml itself return the parameters you need in a tag somewhere, e.g.:

<div id="drawChartValues" parameter1="value1" parameter2="value2" style="display: none;">
</div>

and then in your JavaScript do

function PostStuff() {
  $.post("/GetResult", $('#form01').serialize()).success(function (result) {
    $('#divInputData').attr('style', 'display:none');
    $('#divOutPutData').append(result);
    var p1 = $('#drawChartValues').attr('parameter1');
    var p2 = $('#drawChartValues').attr('parameter2');
    DrawChart(p1,p2);    
};

}

Comments

1

You should keep the div element (for the d3 container) and from your action method, instead of returning a partial view, return the data you need for plotting the graph in JSON format. In your ajax call's success event, read the data and pass that to your js method which renders the d3 grahic

 public ActionResult GetResult(CalculateModel model)
 {
        ResultCalculateModel result = _calculateResult.Calculate(model);
        return Json(result,JsonRequestBehaviour.AllowGet);
 }

And in your main view, Keep the container div

<div id="chartResult"> </div>

And in your js

<script>
 function getResult() {

        $.post("/GetResult", $('#form01').serialize())
            .success(function (result) {
                var s=result.SValue ;
                var p = result.PValu;
                var c =result.CValue;

                drawChart(s,p,c);
        };
   }
   function drawChart(s,p,c){
     //code to render the grpah
   };
</script>

Assuming SValue and PValue and CValue are 3 properties of your ResultCalculateModel

1 Comment

its a good approach, but the problem is that the partial view has a lot of other properties, then i decided to keep it strongly typed for control reasons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.