[英]Exception has been thrown by the target of an invocation MVC
I have an ajax call and I need to get the list of data as JSON from my controller. 我有一个ajax调用,我需要从控制器以JSON格式获取数据列表。 I keep on getting this exception.
我一直在得到这个例外。
Given below is my ajax call: 下面是我的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!!!");
}
});
});
Controller code: 控制器代码:
I can get the parameter value from the ajax and get all values from db. 我可以从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);
}
Exception Message: 异常消息:
Exception has been thrown by the target of an invocation. 调用的目标已引发异常。
Exception Stack trace: 异常堆栈跟踪:
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)
Based on the stack trace, it looks like the error is occurring when MVC tries to JSON-serialize the returned value. 基于堆栈跟踪,当MVC尝试对返回的值进行JSON序列化时,似乎发生了错误。 Since this value is a
List<Property>
, it's likely that the Property
class has a member on it which throws exceptions when invoked under certain circumstances. 由于此值为
List<Property>
,因此Property
类上可能有一个成员,该成员在某些情况下被调用时会引发异常。
Consider creating a separate Data Transfer Object class to return instead of the model class you're using. 考虑创建单独的数据传输对象类以返回而不是使用的模型类。 Or, check the Entity model class to see why invoking its properties might throw an exception.
或者,检查Entity模型类以查看为什么调用其属性可能会引发异常。
As a side note, if your entity model is set up correctly, you should be able to simplify your query logic quite a bit: 附带说明一下,如果您的实体模型设置正确,那么您应该能够大大简化查询逻辑:
using (var db = new LiensTrackerEntities())
{
var pList = db.Properties
.Where(p => p.RecipientPropertiesCollection
.Any(rp => rp.RecipientId == id))
.ToList();
return Json(pList, JsonRequestBehavior.AllowGet);
}
This will complete in a single database round-trip, whereas your current code makes several round trips. 这将在一次数据库往返中完成,而您当前的代码将进行几次往返。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.