【问题标题】:Handling different partial views returning from the same MVC methjod via jQuery Ajax通过 jQuery Ajax 处理从同一 MVC 方法返回的不同部分视图
【发布时间】:2012-07-02 15:22:44
【问题描述】:

设置: 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);
        }
    });

我在考虑可能会为软验证错误返回不同的状态代码,但这感觉很麻烦。

【问题讨论】:

  • 为什么返回不同的状态码会很麻烦? 400“错误请求”将是返回验证错误的正确结果。

标签: asp.net-mvc asp.net-mvc-3 jquery


【解决方案1】:

您可以在响应中再传递一个变量,并通过 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);

    }

【讨论】:

  • 真的有人试过这个吗?它只是不起作用,因为 Jsoning 返回一个对象而不是部分视图 html
  • 这对我有用。本质上,您不能将 PartialView 的结果设置为 htmlToShow。您需要先将部分视图呈现为字符串。我基于此使用了一些东西:stackoverflow.com/questions/19142597/…
【解决方案2】:

您可以执行以下操作。

查看

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多