如何使用mvc和ajax格式将错误消息服务器端传递到客户端

时间:2013-03-07 05:52:30

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

我需要做一些工作:

  1. 我将一些值客户端传递给服务器端(使用ajax的mvc格式)
  2. 在执行某些操作后检查我的控制器中的值,它需要返回在我的客户端找不到的已检查ID(第1条错误消息)。
  3. 如果此条件为真,则表示在下一步中检查另一个条件。
  4. 在这种情况下,检查值为客户提供的消息,例如“你想为同一个学生执行id'是'是'对所有'是'不'对所有人都是'。
  5. 使用此点击操作,我将执行下一步操作。
  6. [我在第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;
            }
    

1 个答案:

答案 0 :(得分:0)

在第一个按钮上点击下面的呼叫功能:     

    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。在这里,您可以检查返回数据,并根据您的条件调用下一个方法。

相关问题