显示 div 以显示操作错误

发布于 2024-11-08 10:55:43 字数 850 浏览 0 评论 0原文

当执行操作时发生异常时,如何在 div 中显示信息?

这是我到目前为止所做的:

<div id="dvErrorMsg">
    <a href="#" class="close">[x]</a>
    <p>
        <label id="errorMessage">
        </label>
    </p>
</div>

<script type="text/javascript">
        $(function () {

            $("#click_here").click(function (event) {
                event.preventDefault();
                $("#dvErrorMsg").slideToggle();
            });

            $("#dvErrorMsg a").click(function (event) {
                event.preventDefault();
                $("#dvErrorMsg").slideUp();
            });
        });
</script>

我的控制器中有这个操作:

public ActionResult Validatecall()
{
    //if any exeption happen show div with the custom error
}

如何捕获任何异常并在 AJAX 响应中发送回消息?

How can I show a div with information whenever an exception occurs when executing my action?

Here is what I have so far:

<div id="dvErrorMsg">
    <a href="#" class="close">[x]</a>
    <p>
        <label id="errorMessage">
        </label>
    </p>
</div>

<script type="text/javascript">
        $(function () {

            $("#click_here").click(function (event) {
                event.preventDefault();
                $("#dvErrorMsg").slideToggle();
            });

            $("#dvErrorMsg a").click(function (event) {
                event.preventDefault();
                $("#dvErrorMsg").slideUp();
            });
        });
</script>

And I have this action in my controller:

public ActionResult Validatecall()
{
    //if any exeption happen show div with the custom error
}

How can I catch any exception and send the message back in the AJAX response?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜还是长夜 2024-11-15 10:55:43

在这种情况下,您可以尝试返回一个 JavaScriptResult,它会告诉您的 div 展开:

public ActionResult Validatecall()
{
    ActionResult result;
    try
    {
       // do whatever
    }
    catch (Exception)
    {
       result = JavaScript("$('#dvErrorMsg').show();$('#errorMessage').val(/* Put exception message here*/")");
    }
    return result;
}

一般来说,我建议使用这种方式与视图交互,因为您现在将依赖项引入到视图上的控制器中其本身如所解释的这里。我提供它作为一种方法,因为我不知道你到底如何调用这个动作。

假设您使用像 post 这样的 ajax 调用来调用此操作,我将返回一个 JsonResult 指示是否发生异常并以这种方式传递错误消息:

$.post("urltoaction", function (result) {    
   if (result.success)
   {
      // do whatever
   }
   else
   {
     //exception occurred show errors
     $('#dvErrorMsg').show();
     $('#errorMessage').val(result.exceptionMsg);
   }  
});

现在是控制器代码:

 public ActionResult Validatecall()
 {
        ActionResult result;
        try
        {
          // do whatever
        }
        catch (Exception e)
        {
           result = new JsonResult()
                    {
                        Data = new { success = false, exceptionMsg = e.Message },
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
        }
        return result;
}

You can try returning a JavaScriptResult in this case that will tell your div to expand:

public ActionResult Validatecall()
{
    ActionResult result;
    try
    {
       // do whatever
    }
    catch (Exception)
    {
       result = JavaScript("$('#dvErrorMsg').show();$('#errorMessage').val(/* Put exception message here*/")");
    }
    return result;
}

Generally, I would not recommend using this way to interact with the view as you now introduce a dependency into the controller on the view itself as explained here. I offered it as a way to do it since I do not know exactly how you are calling this action.

Assuming you are calling this action using an ajax call like post I would return a JsonResult indicating if an exception occurred and pass the error message back that way:

$.post("urltoaction", function (result) {    
   if (result.success)
   {
      // do whatever
   }
   else
   {
     //exception occurred show errors
     $('#dvErrorMsg').show();
     $('#errorMessage').val(result.exceptionMsg);
   }  
});

Here is the controller code now:

 public ActionResult Validatecall()
 {
        ActionResult result;
        try
        {
          // do whatever
        }
        catch (Exception e)
        {
           result = new JsonResult()
                    {
                        Data = new { success = false, exceptionMsg = e.Message },
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
        }
        return result;
}
握住我的手 2024-11-15 10:55:43
try { 
  ... whatever
} catch (Exception $e) {
  $('errorMessage').html($e);
}
try { 
  ... whatever
} catch (Exception $e) {
  $('errorMessage').html($e);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文