1

Setup: ASP.NET MVC3, jQuery, C#

Has anyone got a clean solution to handle different partial views returning from the same action method? One for the next stage, one for returning the view again with validation errors and the other for display an unhandled exception.

I have a controller method that does something like:

public ActionResult SomeMethod(MyModel model)
{

    if(_service.Validate(model))
    {

    if(_service.Update(model))
    {
        // return next view once success
                return PartialView("EverythingIsGood"); // This should be pushed into #somediv  
    }else{
        throw new HardException("Tell the client something bad has happened");
    }
    }
    else
    {
    // Return the same view to highlight the validation errors
        HttpContext.Response.StatusCode = 500;
    return PartialView("SomeMethod", model); // This should be pushed into #anotherdiv  
    }

}

Client Script

 $.ajax({
        url: baseUrl + "Home/SomeMethod",
        type: "GET",
        success: function (data) {
            $("#somediv").html(data);
        },
        error: function (data) {
            handleError(data);
        }
    });

I guessing I need something like softerror:

  $.ajax({
        url: baseUrl + "Home/SomeMethod",
        type: "GET",
        success: function (data) {
            $("#somediv").html(data);
        },
        softerror: function (data) {
            $("#anotherdiv").html(data);
        },
        error: function (data) {
            handleError(data);
        }
    });

I was thinking of maybe returning a different status code for the soft validation errors but this feels hacky.

Dave Hogan
  • 3,201
  • 6
  • 29
  • 54
  • 1
    Why would it be hacky to return a different status code? A 400 "Bad Request" would be the correct thing to return for a validation error. – Blake Pettersson Jul 02 '12 at 15:24

2 Answers2

3

You can pass one more variable in your response and check it value on client side via js. something like this: Controller:

if(_service.Update(model))
{
return Json(new {
        IsEverythingGood=true;
                htmlToShow=PartialView("EverythingIsGood"); // This should be pushed into #somediv
    });
}

...

else
    {
          return return Json(new {
            IsEverythingGood=false;
                    htmlToShow=PartialView("SomeMethod", model); // This should be pushed into #anotherdiv  
    }

and in your javascript:

success: function (data) {
    if(data.IsEverythingGood==true){
        $("#somediv").html(data.htmlToShow);
    }
    else if(data.IsEverythingGood==false){
        $("#anotherdiv").html(data.htmlToShow);

    }
Grinart
  • 276
  • 2
  • 3
  • 9
  • has anyone actually tried this? it just doesnt work, as the Jsoning returns an object instead of the partial view html – YavgenyP Oct 16 '12 at 09:10
  • A variation of this worked for me. Essentially you can't set the result of the PartialView to the htmlToShow. You need to render the partial view to a string first. I used something based on this: http://stackoverflow.com/questions/19142597/render-partial-view-to-string-mvc4 – RikRak Oct 08 '15 at 15:36
1

You can do something like below.

View

 $.get("Home/Index", { random: '@DateTime.Now.Ticks' }, function (response,textData,jqXHR) {
    // Based on **jqXHR.status** value you can fill value of div
    $("#DivName").html(response);
 });

Controller

public ActionResult Index(MyModel model)
{
   if(_service.Validate(model))
    {

    if(_service.Update(model))
    {
        // return next view once success
                return PartialView("EverythingIsGood"); // This should be pushed into #somediv  
    }else{
        throw new HardException("Tell the client something bad has happened");
    }
    }
    else
    {
    // Return the same view to highlight the validation errors
        HttpContext.Response.StatusCode = 500;
    return PartialView("SomeMethod", model); // This should be pushed into #anotherdiv  
    }
}
alok_dida
  • 1,723
  • 2
  • 17
  • 36