2012-07-02 23 views
1

設置: ASP.NET MVC3,jQuery的,C#處理不同的部分景色

有沒有人有一個乾淨的解決方案來處理不同的部分景色,從同樣的動作方法返回?一個用於下一個階段,一個用於返回帶有驗證錯誤的視圖,另一個用於顯示未處理的異常。

我有一個控制器的方法,做一樣的東西:

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 
    } 

} 

客戶端腳本

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

我猜我需要這樣的東西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); 
     } 
    }); 

我正在考慮爲軟驗證錯誤返回一個不同的狀態代碼,但這感覺很糟糕。

+1

它爲什麼會哈克返回不同的狀態代碼?一個400的「錯誤請求」將是返回驗證錯誤的正確的事情。 –

回答

3

你可以在你的迴應傳遞一個多變量,並檢查它通過JS在客戶端的價值。 是這樣的: 控制器:

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 
    } 

,並在你的JavaScript:

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

    } 
+0

有人嘗試過嗎?它只是不工作,因爲Jsoning返回一個對象而不是局部視圖html – YavgenyP

+0

這個變化對我來說很有效。實質上,你不能將PartialView的結果設置爲htmlToShow。您需要首先將局部視圖呈現爲字符串。我使用了基於此的東西:http://stackoverflow.com/questions/19142597/render-partial-view-to-string-mvc4 – RikRak

1

你可以做下面的事情。

查看

$.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); 
}); 

控制器

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 
    } 
} 
相關問題