我有一个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)
答案 0 :(得分:0)
基于堆栈跟踪,当MVC尝试JSON序列化返回的值时,看起来发生错误。由于此值为List<Property>
,因此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);
}
这将在单个数据库往返中完成,而您当前的代码会多次往返。