【问题标题】:Exception has been thrown by the target of an invocation MVC调用 MVC 的目标已引发异常
【发布时间】:2018-02-14 04:20:26
【问题描述】:

我有一个 ajax 调用,我需要从我的控制器中获取 JSON 格式的数据列表。我不断收到这个例外。

下面是我的 ajax 调用:

$('#button1').click(function (e) {
e.preventDefault();
var id= $("#textboxvalue").val();
debugger;
var url = "@Url.Action("GetList", "xyz")";
console.log(url);
$.ajax({
    url: url,
    type: "GET",
    async: false,
    data: { id: id},
    dataType: "json",
    traditional: true,
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        debugger;
        for (var i in data) {       
        }
    },
    error: function (xhr, ajaxOptions, thrownError) {
        //Always goes to error function
        alert("An error has occured!!!");
    }
});
});

控制器代码:

我可以从ajax中获取参数值,从db中获取所有值。

    [HttpGet]
    public ActionResult GetList(string id)
    {

        var pList = new List<Property>();
        using (var db = new LiensTrackerEntities())
        {

            var recipient = db.Recipients.FirstOrDefault(x => x.id== id);
            //Need to make sure there is an Properties object
            if (recipient != null && 
recipient.RecipientPropertyCollections.Count() != 0)
            {
                List<int> properties = 
recipient.RecipientPropertyCollections.Select(p => p.Property_ID).ToList();

                foreach (var item in properties)
                {
                    Property p1 = db.Properties.FirstOrDefault(u => 
u.PropertyID == item);
                    pList.Add(p1);
                }
// I can see the values in pList on continue it goes to ERROR function in 
Ajax with 500 error
                return Json(pList, JsonRequestBehavior.AllowGet);
            }
        }
        return Json(" No spouse info in Medicaid", 
JsonRequestBehavior.AllowGet);
    }

异常信息:

调用的目标已抛出异常。

异常堆栈跟踪:

   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] 
arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, 
Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags 
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Web.SecurityUtils.MethodInfoInvoke(MethodInfo method, Object target, Object[] args)
   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)
   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)
   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)
   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)
   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)
   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)
   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj)
   at System.Web.Mvc.JsonResult.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)

【问题讨论】:

  • 不只是StackTrace异常,你能提供调用它的ToString()方法的结果吗?
  • 你能发布Property类吗?
  • 您的异常可能有内部异常,请检查以获取更多详细信息。
  • 同样在您的请求中,内容类型为 json 但数据不是 json

标签: c# json ajax asp.net-mvc asp.net-mvc-5


【解决方案1】:

根据堆栈跟踪,当 MVC 尝试对返回值进行 JSON 序列化时,似乎发生了错误。由于此值为List&lt;Property&gt;,因此Property 类上可能有一个成员,该成员在某些情况下调用时会引发异常。

考虑创建一个单独的数据传输对象类来返回,而不是您正在使用的模型类。或者,检查 Entity 模型类,了解调用其属性可能引发异常的原因。

附带说明一下,如果您的实体模型设置正确,您应该能够大大简化查询逻辑:

    using (var db = new LiensTrackerEntities())
    {
        var pList = db.Properties
            .Where(p => p.RecipientPropertiesCollection
                .Any(rp => rp.RecipientId == id))
            .ToList();
        return Json(pList, JsonRequestBehavior.AllowGet);
    }

这将在一次数据库往返中完成,而您当前的代码会进行多次往返。

【讨论】:

    猜你喜欢
    • 2019-01-22
    • 2018-11-08
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2011-12-15
    相关资源
    最近更新 更多