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?
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: