开发者

Returning a JSON view in combination with a boolean

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 trie开发者_高级运维d 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.


Yes there is a cleaner way (I think easier too). With the MVC AJAX helper.

For example, in your view:

<% using (Ajax.BeginForm("Edit", new { id = Model.Id},
   new AjaxOptions
   {
       UpdateTargetId = "divId", //id of the container
       OnFailure= "function(){ alert('Error');}" //can be a callback too
   }))
   { %>

Then in your controller:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, Item item)
{
    if (ModelState.IsValid)
    {
        try
        {
            // ...
            return PartialView("Edit", item); //your container will be updated with this content
        }
        catch(Exception ex)
        {
            // ...
            return ReturnStatusCodeError("Item not found", HttpStatusCode.NotFound); //the ajax script will catch this error and fire the OnFailure event
        }
    }

    return ReturnStatusCodeError("Error", HttpStatusCode.Conflict); //can be changed to an error of your preference
}


protected ActionResult ReturnStatusCodeError(string error, HttpStatusCode code)
{
    Response.StatusCode = (int)code;
    Response.Write(error);
    Response.End();

    //redundant. Just for compiler compliance
    return View();
}

This way your container can be automatically updated on submit and success. If there is an error, you can do something more complicated configuring the "OnFailure" ajax option.


Ok, found out why the code on this page:
Render a view as a string
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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜