Ajax 在 URL 中调用自定义模型绑定器返回 FormData
Ajax Call Custom Model Binder returning FormData in URL
我已经为我要通过 Ajax post 访问的复杂集合创建了自定义模型绑定器。一切都很好,直到我 return 将 JsonResult 添加到页面,然后整个 FormData 被附加到 URL。有没有我遗漏的小东西?
我有一些ajax到post的表格。
$(document).ready(function() {
$("#SaveButton").click(function(e) {
var form = $("#myForm");
$.ajax({
type: "POST",
url: '/myController/SaveAll',
data: form.serialize(),
dataType: "json",
error: function(xhr) {
console.log('Error: ' + xhr.statusText);
},
success: function(result) {
console.log(result);
},
async: true
});
});
});
控制器动作。
public JsonResult SaveAll([ModelBinder(typeof(CustomModelBinder))]ProvinceListViewModel model)
{
// process something
return this.Json(new
{
Sucess = true
}, JsonRequestBehavior.AllowGet);
}
这是我使用 request.FormData
的自定义模型活页夹
public class CustomModelBinder : DefaultModelBinder
{
private NameValueCollection _formCollection;
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext.ModelType == typeof(ProvinceViewModel))
{
HttpRequestBase request = controllerContext.HttpContext.Request;
// set the form collection
this._formCollection = request.Form;
// build the view models, parse form collection
// return the tab container view model
return new ProvinceViewModel();
}
else
{
return base.BindModel(controllerContext, bindingContext);
}
}
}
结果 url 附加了表单数据。我该如何防止这种情况?提前致谢。
_RequestVerificationToken=PK4YYR1fTh15rpHQwE883NlVOLho7LLWL7cdH_3jP0lq8SXhKGvOHq7imuBUf-xr6sOP5dIMbVMPVcPuA1Rsgt616x3Tub4DK57VCGZ4-oo1&MyId=
尝试像这样修改您的 post:
$.ajax({
type: "POST",
url: '/myController/SaveAll',
data: {'Data':form.serialize()},
dataType: "json",
error: function(xhr) {
console.log('Error: ' + xhr.statusText);
},
success: function(result) {
console.log(result);
},
async: true
});
问题是我在表单中有一个提交按钮并将 onclick 事件附加到它。我将其更改为 div 元素,问题就解决了。
我已经为我要通过 Ajax post 访问的复杂集合创建了自定义模型绑定器。一切都很好,直到我 return 将 JsonResult 添加到页面,然后整个 FormData 被附加到 URL。有没有我遗漏的小东西?
我有一些ajax到post的表格。
$(document).ready(function() {
$("#SaveButton").click(function(e) {
var form = $("#myForm");
$.ajax({
type: "POST",
url: '/myController/SaveAll',
data: form.serialize(),
dataType: "json",
error: function(xhr) {
console.log('Error: ' + xhr.statusText);
},
success: function(result) {
console.log(result);
},
async: true
});
});
});
控制器动作。
public JsonResult SaveAll([ModelBinder(typeof(CustomModelBinder))]ProvinceListViewModel model)
{
// process something
return this.Json(new
{
Sucess = true
}, JsonRequestBehavior.AllowGet);
}
这是我使用 request.FormData
的自定义模型活页夹 public class CustomModelBinder : DefaultModelBinder
{
private NameValueCollection _formCollection;
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext.ModelType == typeof(ProvinceViewModel))
{
HttpRequestBase request = controllerContext.HttpContext.Request;
// set the form collection
this._formCollection = request.Form;
// build the view models, parse form collection
// return the tab container view model
return new ProvinceViewModel();
}
else
{
return base.BindModel(controllerContext, bindingContext);
}
}
}
结果 url 附加了表单数据。我该如何防止这种情况?提前致谢。 _RequestVerificationToken=PK4YYR1fTh15rpHQwE883NlVOLho7LLWL7cdH_3jP0lq8SXhKGvOHq7imuBUf-xr6sOP5dIMbVMPVcPuA1Rsgt616x3Tub4DK57VCGZ4-oo1&MyId=
尝试像这样修改您的 post:
$.ajax({
type: "POST",
url: '/myController/SaveAll',
data: {'Data':form.serialize()},
dataType: "json",
error: function(xhr) {
console.log('Error: ' + xhr.statusText);
},
success: function(result) {
console.log(result);
},
async: true
});
问题是我在表单中有一个提交按钮并将 onclick 事件附加到它。我将其更改为 div 元素,问题就解决了。