views:

65

answers:

1

What i would like to accomplish is that a partiel view contains a form. This form is posted using JQuery $.post. After a successfull post javascript picks up the result and uses JQuery's html() method to fill a container with the result.

However now I don't want to return the Partial View, but a JSON object containing that partial view and some other object (Success -> bool in this case).

I tried it with the following code:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(int id, Item item)
    {
        if (ModelState.IsValid)
        {
            try
            {
                // ...
                return Json(new
                {
                    Success = true,
                    PartialView = PartialView("Edit", item)
                });
            }
            catch(Exception ex)
            {
                // ...
            }
        }

        return Json(new
        {
            Success = false,
            PartialView = PartialView("Edit", item)
        });
    }

However I don't get the HTML in this JSON object and can't use html() to show the result. I tried using this method to render the partial as Html and send that. However this fails on the RenderControl(tw) method with a: The method or operation is not implemented.

A: 

Ok, found out why the code on this page:
http://stackoverflow.com/questions/483091/render-a-view-as-a-string/1241257#1241257
didn't worked.

Stupid: I had to include System.Web.Mvc.Html to use the renderpartial on the HtmlHelper object.

However I'm not really convinced by this solution. There must be a cleaner way?

Rody van Sambeek