【问题标题】:How to pass the error message server side to client side using mvc and ajax format如何使用 mvc 和 ajax 格式将错误消息服务器端传递到客户端
【发布时间】:2013-02-22 05:33:21
【问题描述】:

我需要一些工作如下:

  1. 我将一些值客户端传递到服务器端(使用 ajax 的 mvc 格式)
  2. 在某些操作后检查我的控制器中的值,它需要返回在我的客户端未找到的检查 id(这是第一个错误消息)。
  3. 如果此条件为真,则意味着在下一步中检查另一个条件。
  4. 在这种情况下,请检查该值是否为客户端提供消息,例如“您是否愿意为同一个学生执行 id '是' 对所有都是' '否' '对所有都没有'。
  5. 使用此单击操作我将执行下一个操作。

[我对第 4 步和第 5 步很困惑]。我只是下面给出的示例代码括号,请转发您的大知识。我知道这是凝灰岩之一。谢谢你

我的代码:

     public ActionResult Action(parameters)
     {
            foreach (var seletedid in id)
            {
                // my code
            }    

            if (1st condition )
            {    
                return Json(new { success = false }); 
                // 1st error message i use like this
                // after that the preform goes to the end
            }
              try
            {                   
                foreach (some code)
                {                      
                  // my code
                    if (2nd condtion (3 point))
                    {
                     // using this i perform the some action herer 
                     }    
                }
           }
              catch (Exception ex)
              {    
                  return null;
              }
              return null;
        }

【问题讨论】:

    标签: c# ajax asp.net-mvc exception error-handling


    【解决方案1】:

    在第一个按钮点击调用下面的函数:

        function MyFunction() {
            $.post('@Url.Action("Action", "Controller")', { pareter1: value1, pareter2:     value2 }, function (json) {
                if (json.Status) {
                    var confirm = confirm("would you like perform the id for same student?");
                if (confirm) {
                    $.post('@Url.Action("SecondAction", "Controller")', { pareter3: value3, pareter4: value4 }, function (result) {
                        //check the condition and proceed here, if needed.
                    });
                }
            });
    
        }
    </script>
    

    如果您正在使用:

    Ajax.BeginForm("Action", "ControllerName", new AjaxOptions { OnSuccess = "SuccessNotification" })
    

    然后做:

    function SuccessNotification(json) {
            if (json.Status) {
                var confirm = confirm("would you like perform the id for same student?");
                if (confirm) {
                    $.post('@Url.Action("SectionAction", "Controller")', { pareter3:     value3, pareter4: value4 }, function (result) {
                        //check the condition and proceed here, if needed.
                    });
                }
            }
    
        }
    

    当服务器端返回某些内容时,将调用SuccessNotification。在这里您可以检查返回数据,并根据您的情况调用 next 方法。

    【讨论】:

    • 如何先将数据服务器传给客户端?
    • return Json(new { success = false });将数据返回到客户端。您可以在此处添加参数数量。像: return Json(new { success = false,Message="Validated Successfully" }) 并且可以在成功方法上获得这些输出。 eg: 在 SuccessNotification(json): json.success 和 json.Message
    • 感谢它的有用。但我主要在服务器端挣扎。在我的代码中,如果(第一个条件)之后如何传递值(未选择的 id)。这里我只是从我的服务器端传递错误
    • 您可以从服务器传递到客户端返回 Json(new { success = false,UnSelectedId=yourid }) 并且可以作为 json.UnSelectedId 进入客户端
    • 谢谢你我已经完成了。但是在最后一行 retun null 我们不需要但是当我删除行错误时显示“必须返回操作结果”如何解决这个问题?
    猜你喜欢
    • 2010-09-24
    • 1970-01-01
    • 2018-05-10
    • 2013-09-03
    • 2013-04-24
    • 1970-01-01
    • 2022-01-01
    • 2014-01-05
    • 1970-01-01
    相关资源
    最近更新 更多