Returning both status and HTML from Ajax Post in ASP.NET MVC

I am showing a page where part of the page is FORM - rendering via partial. The submit button of this form submits to my controller via Ajax (using JQuery.post ()). When a controller action occurs, there are two possible paths of execution:

  • These forms are valid. In this case, the controller updates the database with a new record and then returns the HTML table by rendering a PartialView (and returns a ViewResult).

  • These forms are not valid. In this case, I want to re-render the original form - with validation errors. I believe I can do this with another call to PartialView.

On the client side, in the completion javascript function, I have to distinguish between success and failure cases because the DOM target for the returned HTML will be different. I think I could check the HTML for some known element to distinguish cases, but I thought the best solution would be to return a JSonResult that contains boolean state in one field and HTML in another.

I know how to return JsonResult from controller. However, I need to be able to display HTML from PartialView calls in order to insert that text into a field. Somebody knows:

(a) how to do it, and / or

(b) the best way to approach the situation.

TIA

Update 8/20/2009

I think I'm getting closer to what I want with this code:

        ViewEngineResult viewEngineResult = ViewEngines.Engines.FindPartialView(this.ControllerContext, "HospitalDoseList");
        ViewData.Model = hospitalStay;
        ViewContext viewContext = new ViewContext(ControllerContext, viewEngineResult.View, this.ViewData, this.TempData);
        using (StringWriter writer = new StringWriter()) {
            viewEngineResult.View.Render(viewContext, writer);
            string html = writer.ToString();
            JsonResult jsonResult = new JsonResult();
            jsonResult.Data = new {Status = true, Html = HttpUtility.HtmlEncode(html)};
            return jsonResult;
        }

      

However, the html string goes down to zero and I expected it to contain HTML which would normally be displayed in the response stream via a call

return PartialView("HospitalDoseList", hospitalStay);

      

+2


source to share


2 answers


I would guess the easiest would be to do a separate validation and send messages triggered by the submit button. You have to do AJAX validation / post first, which returns JSON status and possibly an array of validation errors (strings). In fact, an empty list will probably suffice to prove success, so you can omit the status. Once you've done your server side validation, then call another action via AJAX that will fetch the data for display. This action will render the partial view and return HTML.



+1


source


I was going to stick with this in the comment, but it was too much text ...

AJAX status code return pattern is very common, check out my post here: jQuery AJAX Answers in ASP.NET MVC and this blog post: http://www.bennadel.com/blog/1392-Handling-AJAX-Errors-With-jQuery .htm .



As for the partial view, you're a little out of luck - ViewResults have to call ExecuteResult (), which then renders the html directly into the response. You need to come up with a different strategy, perhaps something like tvanfosson.

+1


source







All Articles