Is it possible to program the Razor programmatically compiler from a controller method?

I am using ASP.NET MVC 3, and I have an interesting problem to solve, which I hope for some tips.

I have a page that has multiple divs. The content of each div changes over time, and therefore, I currently have a timer for each start of the div that requests $ .ajax to the server, which returns a PartialViewResult with the updated contents of the div. The partial view is quite complex and refers to other views.

The problem with this approach is that it does not scale very well. Perhaps each user has many of these timers, and with a large number of users, the server constantly gets into. Therefore, I would prefer to make one request to the server, which returns, possibly, several div contents in such a way that it will be:

div1 { some html } div2 { some html } 

...

Then on the client, I could put every bit of HTML at the right position on the page.

I thought that what I can do is return the JSON from the server, but my problem is how do I get the HTML? At the moment, the razor compiler will start and turn my partial cshtml files into HTML, but if I return JSON, can I call the razor compiler programmatically?

I found the Razor Engine here: http://razorengine.codeplex.com/ , which seems to do what I want, but is it possible to do this using only vanilla ASP.NET MVC?

Or, given the problem, is there a better way to achieve my goal?

Thanks for any help!

+8
c # asp.net-mvc asp.net-mvc-3 razor
source share
3 answers

Create an action that returns a new PartialView that displays all of these PartialViews. for example action:

 public PartialViewResult AggregatedAction(args) { return PartialView(); } 

with a view that contains:

 @Html.Action("IndividualAction1", null) @Html.Action("IndividualAction2", null) @Html.Action("IndividualAction3", null) 

See http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx for more details.

Thus, there is only one request , and the visualization engine is called from the right place, that is, the view.

Then with the result, you can search for various divs and replace the html in the client.

 $('div#id1').html('div#id1',$(data)); $('div#id2').html('div#id2',$(data)); 

If your page structure allows this, you should use: http://api.jquery.com/load/ (as @Jorge says) to replace all html with one line.

 $('div#targetDiv').load('Controller\AggregatedAction', anyData); 
+5
source share

You can have two methods: one that returns HTML, and the other that returns JSON.

Or, alternatively, create an ActionResult that delegates to JsonResult if the request is an Ajax or PartialViewResult request, otherwise, for example:

 public class AjaxableResult : ActionResult { private readonly JsonResult _jsonResult; private readonly PartialViewResult _partialViewResult; public AjaxableResult(JsonResult jsonResult, PartialViewResult partialViewResult) { _jsonResult = jsonResult; _partialViewResult = _partialViewResult; } public override void ExecuteResult(ControllerContext context) { if (context.HttpContext.Request.IsAjaxRequest()) { _jsonResult.ExecuteResult(context); } else { _partialViewResult.ExecuteResult(context); } } } 
+3
source share

Why are you just forcing you to call ajax and instead of expecting the JSON object to send the html to the client using the method in your ActionResult controller, remember that this type returns an html of type like this

  //this if you want get the html by get public ActionResult Foo() { return View(); } 

And the customer called it

 $.get('your controller path', parameters to the controler , function callback) 

or

 $.ajax({ type: "GET", url: "your controller path", data: parameters to the controler dataType: "html", success: your function }); 

You can also load partial views and render in certain parts of your view using jquery load , which it is called no more than ajax

+1
source share

All Articles