<div id="updatePanel"> </div>
我的JS脚本:
<script type="text/javascript">
$("#MyDropDownID").change(function () {
$.ajax({
url: "@Url.Action("MyMethodToGetList","MyController")",
contentType: "application/json; charset=utf-8",
type: 'GET',
dataType: 'json',
success: function (data) {
loadData(data);
},
error: function (xhr, status, error) {
alert(error);
}
});
});
function loadData(data) {
var tab = $('<table class="myTable"></table>');
var thead = $('<thead></thead>');
thead.append('<th>Name</th>');
tab.append(thead);
$.each(data, function (i, val) {
var trow = $('<tr></tr>');
trow.append('<td>' + val.Name + '</td>');
tab.append(trow);
});
$("tr:odd", tab).css('background-color', '#C4C4C4');
$("#updatePanel").html(tab);
};
});
</script>
控制器:
[WebMethod]
public JsonResult MyMethodToGetList()
{
List<T> list = Class1.ReturnListWithThisParam(1);
return Json(list, JsonRequestBehavior.AllowGet);
}
我试图获得一个List&lt;&gt;使用JSON,..
我在我的方法中设置断点,它可以工作,它检索1个元素,但在浏览器中我只是失败警报,为什么?我做错了什么?
其他方式(不适用):
<script type="text/javascript">
$('#MyDropDownId').change(function () {
$.getJSON('@Url.Action("MyMethodToGetList", "MyController")', function(result) {
var ddl = $('#MyDropDownId');
ddl.empty();
$(result).each(function() {
ddl.append(
$('<option />', {
value: this.Id
}).html(this.Name)
);
});
});
</script>
我试图用JSON填充我的DropDown:
@Html.DropDownListFor(x => x.MyProperty, new SelectList(Enumerable.Empty<SelectListItem>()))
我的模特:
[MetadataType(typeof(SellerMetadata))]
public partial class Seller
{
public Seller()
{
this.SaleRecords = new HashSet<SaleRecord>();
}
public int Id { get; set; }
public string Name { get; set; }
public string CPF { get; set; }
public string RG { get; set; }
public DateTime AdmissionDate { get; set; }
public Situation Situation { get; set; }
public Scholarity Scholarity { get; set; }
public int DeptId { get; set; }
public virtual Department Department { get; set; }
public virtual ICollection<SaleRecord> SaleRecords { get; set; }
}
我在浏览器中检查过,我收到了这个错误:
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
em System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller)
em System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
em System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
em System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
em System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag)
em System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
em System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
em System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
em System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
em System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
em System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
em System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag)
em System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
em System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
em System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
em System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
-->
答案 0 :(得分:1)
试试这个
$.ajax({
type: 'GET',
url: '/yourController/yourMethod',
contentType: "application/json",
dataType: "json",
success: function(data) {
alert('success');
},
error: function() {
alert('fail');
}
});
public ActionResult yourMethod()
{
return Json("1", JsonRequestBehavior.AllowGet);
}
答案 1 :(得分:0)
刚发现错误: 我在控制器方法中添加了它:
myContext.Configuration.ProxyCreationEnabled = false;