0

can you clarify a little bit how returning raw data from a partial view works in practice?

@Html.Partial("_MyPartialView") returns always the raw data so that I can see the HTML in the page source.

But if I'm trying to do the same via JQuery AJAX call I cannot see the HTML anymore in the page source

$.ajax({
url: '@Url.Action("GetData", "Home")',
data: { Period: period, FromDate: fromDate, ToDate: toDate },
type: 'GET',
success: function (data) {
$("#someDiv").html(data);
}
}); 

Both examples work but the difference is that I cannot see the HTML output anymore via AJAX. Is this by design and does it really matter? Or can I generate visible HTML in the output via AJAX?

2
  • Can you show your action's code? Commented Oct 21, 2013 at 13:17
  • Maybe you should define request type? type: 'text/html'. Commented Oct 22, 2013 at 7:39

2 Answers 2

2

Page source is what you would see for the first page loaded. Ajax loads HTML dynamically, you won't see it in the page source, but it's actually loaded and attached to your DOM, you can inspect dynamic html using HTML inspection tool in browser

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, this was the answer I was looking for.
Do you know if I can inspect dynamic html using the Page inspector in Visual Studio 2012? I tried it quickly but couldn't see any way to see the dynamic html inside the div. I would definitely need an HTML inspection tool. Any recommendations for a Visual Studio developer?
@user405723: I recommend that you should run your HTML inside a browser as browsers have better tools to deal with HTML. As a Visual studio developer, you could easily run the built-in web server to serve your HTML pages.
I tried to inspect the dynamic html inside the div using Firebug DOM Inspector and IE10 Developer tools but with no luck. Both tools just show empty content inside the div. Any advice?
Found a better way. I can see the dynamic html by inspecting the Ajax response with Fiddler Web Debugger. Shows beautifully the whole content from a partial view.
2

The difference is in number of HTTP calls. With PartialView you are getting HTML from this view during the rendering your main page. And HTML is returned in a place where you called Html.Partial. In the second example with AJAX you are returning HTML code of the main page without code from PartialView. Then after your page is loaded, it initiates another HTTP request to get the markup from PartialView. If you are not planning to update the content of your #someDiv programmatically from the JavaScript, I would recommend to go with first approach, since it will produce less HTTP requests and less traffic on your server, client will retrieve entire page faster.

Comments

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.