Ross Bush
Ross Bush

Reputation: 15175

Asp.Net MVC and Partial Views and Mixing Ajax.BeginForm and Html.BeginForm

I have a some partial views that generally make use of small model data. I have been using Ajax.BeginForm to with js functions defined in the AjaxOptions to catch the partial postback's success or failure when that part is submitted.

I saw no performance penalty as my data has been light. Currently, I have been working on a piece that returns an HTMLFragment from an SSRS report rendered as format "HTML40" in the controller. The JSON serialization of the html portion is taking a long time and I no longer see it as an option. When I changed the Ajax.BeginForm to Html.BeginForm and rendered the report upfront then the payload is lighter and takes less time to render but now I am stuck. How do I issue refresh commands without loading the partial again? This is why I used Ajax.BeginForm in the first place :( Perhaps there is a better way to load the data than what I am doing. I have started looking into callbacks and somehow getting the report pieces a raw byte[] or string as opposed to using json serialization.

Thanks

Upvotes: 0

Views: 887

Answers (1)

bobek
bobek

Reputation: 8020

You can load a whole partial into a div using .load() jQuery function:

$("#someButton").click(function(){
  $("#someDive").load("/url", {type: 'POST'}, function(){
  });
});

Now, when you click on #someButton, an AJAX call will be made to /url. The /url can return a PartialView that will be loaded directly into your div:

//controller URL
[HttpPost]
public ActionResult Index(){
    return PartialView("MyPartialView");
}

You can pass in models as you'd do to regular view.

Upvotes: 2

Related Questions