我在使用ajax jquery向MVC 4控制器发布多个对象时遇到了问题。已经过了几周了,但我似乎找不到解决办法。
我尝试了几种方法,有时filterModel对象为空,有时字符串参数为空(无论我是否stringify或指定contentType)。
我想要什么? 我想传递三个对象:1.filterModel 2.testparamA 3.testparamB。 我该怎么做才能将这三个对象全部传递给MVC控制器?我需要在data:中编写什么来获取所有3个对象的值?
最简单的控制器
最简单的视图
最简单的FilterModel类
我尝试了几种方法,有时filterModel对象为空,有时字符串参数为空(无论我是否stringify或指定contentType)。
我想要什么? 我想传递三个对象:1.filterModel 2.testparamA 3.testparamB。 我该怎么做才能将这三个对象全部传递给MVC控制器?我需要在data:中编写什么来获取所有3个对象的值?
最简单的控制器
[HttpPost]
public JsonResult Test(string testparamA, string testparamB, FilterModel filter)
{
using (RBSystemEntities repository = new RBSystemEntities())
{
return Json(new {
DataList = repository.Items.Select(x => new {x.PKID, x.ItemName}).ToList(),
Result = "OK"
});
}
}
最简单的视图
var filterModel = @Html.Raw(Json.Encode(new FilterModel("ItemName", "Pepperoni Pizza")))
//filterModel = JSON.stringify(filterModel);
function testme() {
// post the javascript variable back to the controller
$.ajax({
url: '/Menu/Test',
type: 'POST',
//contentType: 'application/json; charset=utf-8',
data: {
filter: filterModel,
testparamA: 'A value',
testparamB: 'B value'
}, // with this approach I get filterModel null in the controller however testparamA and testparamB has values
data: filterModel, // with this approach I get values for filterModel but I can't pass testparamA and testparamB
success: function (result) {
// TODO: do something with the results
alert('success');
}
});
}
testme();
最简单的FilterModel类
public class FilterModel
{
public FilterModel() { }
public FilterModel(string filtercolumn, string filtervalue)
{
this.FilterColumn = filtercolumn;
this.FilterValue = filtervalue;
this.FilterColumnCriteria = "=";
}
public string FilterColumn { get; set; }
public string FilterValue { get; set; }
public string FilterColumnCriteria { get; set; }
}
FilterModel
编写了 ModelBinder 吗? - Philipp MFilterModel
对象和平面参数,我认为,但我现在无法测试它。 - Philipp M