显示 div 以显示操作错误
当执行操作时发生异常时,如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,您可以尝试返回一个 JavaScriptResult,它会告诉您的 div 展开:
一般来说,我不建议使用这种方式与视图交互,因为您现在将依赖项引入到视图上的控制器中其本身如所解释的这里。我提供它作为一种方法,因为我不知道你到底如何调用这个动作。
假设您使用像 post 这样的 ajax 调用来调用此操作,我将返回一个 JsonResult 指示是否发生异常并以这种方式传递错误消息:
现在是控制器代码:
You can try returning a JavaScriptResult in this case that will tell your div to expand:
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:
Here is the controller code now: