Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PartialView do what I think it should (but doesn't)?

I have a div on an ASP.NET MVC page that I would like to populate dynamically (at user request) using jQuery. I currently have jQuery placing a JSON call to my controller, which returns a JSON object, and then I use Javascript to build the HTML manually, which is then placed in the div.

Hang on a minute. Wouldn't it be much easier to get the controller to produce the HTML on its own (using a custom control (.ascx file)), and then just return the string to be placed in the div?

Easier, schmeasier!

My current attempt involves the following javascript:

$('#MyDiv').load("/MyController/GetList");

calling the following controller method:

public PartialViewResult GetList()
{
    ... create model ...
    var result = PartialView("CategoryList", model);
    return result;
}

Problem is, I get absolutely no response from the controller. It is called correctly (as proven by a breakpoint), but Firebug doesn't even register a call to the controller in Net view (although the call does show up the Firebug Console window, with a blank response).

Further, a Debug.Print command within the .ascx file produces an output, so the jQuery call and the PartialView method definitely fire.

What is PartialView (and PartialViewResult) used for, and what should I be doing instead of this?

Thanks in advance.

Oh, and it's using the Release Candidate... if that makes a difference.

like image 566
Darren Oster Avatar asked Dec 03 '25 22:12

Darren Oster


1 Answers

In one of the most bizarre fixes I've found yet, it would seem that (in my case), adding a

<html><head><title></title></head><body>

before the user control code, and a

</body></html>

after the user control code, gets around the problem. Without these, the control returns nothing.

Now, it is perfectly possible that this is due to some strange issue caused by the site's evolving from Preview 3 -> Preview 4 -> Preview 5 -> Beta -> RC1, so I wouldn't be surprised if I am the ONLY PERSON IN THE WORLD getting this issue, but there you have it.

If anyone does find a better answer, please add it here...

like image 99
Darren Oster Avatar answered Dec 05 '25 16:12

Darren Oster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!