views:

296

answers:

2

I see there is a question here but there is no definite answer. Has anyone any ideas how to return a PartialView with Javascript or JSON. I am doing an AJAX post, on success it renders the PartialView but then needs to run some javascript or check the JSON result.

A: 

I think the answer provided for the other question may be your best option. You can not suddenly have another onSuccess method signature where another parameter is added with a JSON object, so you are stuck with one return object. Logically, this object then must contain both your view and your JSON object, which implies that the object itself must be a JSON object.

Ronald Wildenberg
When the post is invoked to the controller it is via jQuery AJAX. The controller returns HTML. A JSON object contains data about what has happened as well as any errors occured. The OnSuccess event can update a div with the PartialView as it writes to the response so this is what is outputted, however, I need to do something with a JSON object
Jon
So the JSON object is only returned in case of an error condition?
Ronald Wildenberg
No, its returned every time, it will contain simple data such as "person updated" or more complex data if errors occur
Jon
Ok, that may be impossible to do in a really clean way. Maybe your comment on the original question will provide some additional insights....
Ronald Wildenberg
The overall goal is to have a table/grid with a list of items which is paginated. You click a delete button on a row which does a post, removes the item, returns the list with the removed item and also a message saying person deleted. That seems quite standard functionality but hard to implement at the moment. I do have a way of doing it by adding data to the ViewData then doing the PartialView which reads the ViewData but I don't like it for some reason.
Jon
I thought I had another solution but that will break the paging stuff... Still thinking about a nicer solution.
Ronald Wildenberg
Like I say I can do it but its as pretty as I'd like. The one option I have thought about is to somehow get the HTML returned from the PartialResult and put that into a JSON object and do a JSONResult. I'm not sure thats great but would work and not sure how you get the HTML from a PartialViewResult
Jon
Unfortunately I also don't know how to get the html from a PartialViewResult. But this should be possible since ASP.NET MVC must do something similar somewhere... Maybe post another question asking this specific question.
Ronald Wildenberg
I have just found this but again, no answer - http://stackoverflow.com/questions/908450/asp-mvc-view-content-as-json
Jon
why dont you just use ajax to get a HTML result rather than JSON?.. this would be much easier to render into your page.
cottsak
But the whole idea is to get both JSON and html from the server.
Ronald Wildenberg
i just dont understand why you want to serialise html data to JSON just so u can deserialise it in the browser to html again. it seems silly to me. (i dont mean to judge). if its HTML coming to the browser, send it as HTML, if its control data, or other (lighter) specialised data, use JSON - thats what its for. you sound like you're trying to make a headache for yourself IMHO.
cottsak
Take a look at http://stackoverflow.com/questions/908450/asp-mvc-view-content-as-json which might explain it better for you
Jon
+1  A: 
public static string RenderPartialToString(string controlName, object viewData, object model, System.Web.Routing.RequestContext viewContext)
            {

                ViewDataDictionary vd = new ViewDataDictionary(viewData);
                ViewPage vp = new ViewPage { ViewData = vd };

                vp.ViewData = vd;
                vp.ViewData.Model = model;
                vp.ViewContext = new ViewContext();
                vp.Url = new UrlHelper(viewContext);

                Control control = vp.LoadControl(controlName);

                vp.Controls.Add(control);

                StringBuilder sb = new StringBuilder();

                using (StringWriter sw = new StringWriter(sb))
                {

                    using (HtmlTextWriter tw = new HtmlTextWriter(sw))
                    {

                        vp.RenderControl(tw);

                    }

                }

                return sb.ToString();

            }
Jon
I see you found a solution. Found it somewhere else or came up with this yourself? Just curious...
Ronald Wildenberg
Took it from here and modified it slightly - http://thriftybliss.spaces.live.com/blog/cns!58DA805F37F31F20!170.entry
Jon