无法从控制器获取异常 [英] unable to get exception from controller to view

查看:116
本文介绍了无法从控制器获取异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$ c $ 

c> ** VIEW:**



$(#txtToloc)。change(function(event){

var toloc = $('#txtToloc')。val();
var mn =<%= new JavaScriptSerializer()。Serialize(ViewData [MODELNUMBER])%> ;;
var fm =< %= new JavaScriptSerializer()。Serialize(ViewData [lblocation])%> ;;
var it =<%= new JavaScriptSerializer()。Serialize(ViewData [lbinvtype])%> ;;
var whid =14;
debugger;

$ .ajax({
url:'<%:Url.Action(GetPartialGraph)%> ;',
data:{'Tolocation':mn,'Frmlocation':fm,'moNo':it,'whid':whid},
type:post,
cache :false,
dataType:html,
success:function(result){
alert('yeap');
},
error:function(xhr,status,error){
alert('loss');
}
});
});
});

从控制器中调用操作方法。

  ** CONTROLLER。** 



public ActionResult GetPartialGraph(string Tolocation,string Frmlocation,string moNo,string whid )
{

string isvalid =0;
if(Frmlocation.ToUpper()。Trim()== Tolocation.ToUpper()。Trim())
{
}
else
{
try
{
ut.Setlocation(Tolocation,Frmlocation,moNo,whid);
}

catch(iDB2Exception ex)
{
/ * in catch取决于某种条件我想通过diff异常* /

return View(isvalid);
}


一切正常。但我不能把异常回来查看。向客户端显示正确的消息。从

错误:function(xhr,status,error){
alert('loss');
}

我打算调用另一个函数,消息给用户。 Iam无法从控制器返回异常以显示在视图中。

解决方案

您正在返回一个视图,因此错误执行。您可以将状态代码设置为500并返回JSON结果:

  catch(iDB2Exception ex)
{
Response.StatusCode = 500;
Response.TrySkipIisCustomErrors = true;
return Json(new {errorMessage = ex.Message},JsonRequestBehavior.AllowGet);
}

,然后在错误处理程序中读取这个值:

 错误:function(xhr){
if(xhr.getResponseHeader('Content-Type')。indexOf('application / json' )> -1){
var json = $ .parseJSON(xhr.responseText);
alert(json.errorMessage);
}
}


I used on change event to textbox in mvc everything works fine but I am unable to handle exception throw by controller in view.

**VIEW:**



 $("#txtToloc").change( function (event) {

        var toloc= $('#txtToloc').val();
                    var mn = <%= new JavaScriptSerializer().Serialize(ViewData["MODELNUMBER"])%>;
                    var fm = <%= new JavaScriptSerializer().Serialize(ViewData["lblocation"]) %>;
                    var it = <%= new JavaScriptSerializer().Serialize(ViewData["lbinvtype"])%>;
                    var whid = "14";
                    debugger;

            $.ajax({
                url: '<%: Url.Action("GetPartialGraph")%>',
                data: { 'Tolocation' :mn, 'Frmlocation' :fm, 'moNo' :it, 'whid' : whid },
                type: "post",
                cache: false,
                dataType: "html",
                 success: function(result) {
                 alert('yeap');
                  },
       error: function(xhr, status, error) {
                  alert(‘loss’);
                }
            });
        });
        });

From view i am calling action method in controller.

    **CONTROLLER.**



  public ActionResult GetPartialGraph(string Tolocation, string Frmlocation, string moNo, string whid)
            {

                string isvalid = "0";
                if (Frmlocation.ToUpper().Trim() == Tolocation.ToUpper().Trim())
                {
                }
                else
                {
                    try
                    {                    
                        ut.Setlocation(Tolocation,Frmlocation,moNo,whid);
                    }

                    catch (iDB2Exception ex)
                    {
                       /* in catch depending upon certain condition I want through diff exception */

                           return View(isvalid);  
                     }


 Everything works fine. But I am unable to bring exception back to view. To show proper message to client. From 

error: function(xhr, status, error) {
              alert(‘loss’);
            }

I am going to call another function and depending upon return I am going to show message to user. Iam unable to return exception from controller to show in view.

解决方案

You are returning a view, so the error function will never be executed. You could set the status code to 500 and return a JSON result:

catch (iDB2Exception ex)
{
    Response.StatusCode = 500;
    Response.TrySkipIisCustomErrors = true;
    return Json(new { errorMessage = ex.Message }, JsonRequestBehavior.AllowGet);
}

and then in your error handler simply read this value:

error: function (xhr) {
    if (xhr.getResponseHeader('Content-Type').indexOf('application/json') > -1) {
        var json = $.parseJSON(xhr.responseText);
        alert(json.errorMessage);
    }
}

这篇关于无法从控制器获取异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆