AJAX后多数据来ASP.Net MVC [英] AJAX Post Multiple Data to ASP.Net MVC
问题描述
我现在面临的问题,同时张贴通过AJAX的jQuery多个对象MVC 4控制器。它一直星期,但我似乎无法找到解决的办法。
我试了几种方法,有时filterModel对象为null,有时字符串参数为空(不事我就算字符串化,如果我指定的contentType与否)
我想要什么?
我想通过三个对象:1。filterModel 2.testparamA 3.testparamB
我应该怎么做才能通过全部三个物体MVC控制器?我需要什么数据写:所以我得到所有3个对象的值
最简单的控制器
[HttpPost]
公共JsonResult测试(字符串testparamA,串testparamB,FilterModel过滤器)
{
使用(RBSystemEntities库=新RBSystemEntities())
{
返回JSON(新{
DataList控件= repository.Items.Select(X =>新建{x.PKID,x.ItemName})了ToList()
结果=OK
});
}
}
最简单的查看
VAR filterModel = @ Html.Raw(Json.En code(新FilterModel(ITEMNAME,辣香肠比萨饼)))
// filterModel = JSON.stringify(filterModel);功能TESTME(){
//发布JavaScript变量回控制器
$阿贾克斯({
网址:'/菜单/测试,
输入:POST,
//的contentType:应用/ JSON的;字符集= UTF-8,
数据:{
过滤:filterModel,
testparamA:'值',
testparamB:B值
} //使用这个方法,我得到filterModel空控制器然而testparamA和testparamB具有价值
数据:filterModel,//使用这个方法,我得到filterModel值,但我无法通过testparamA和testparamB
成功:函数(结果){
// TODO:做的事结果
警报(成功);
}
});
}
TESTME();
最简单的FilterModel类
公共类FilterModel
{
公共FilterModel(){}
公共FilterModel(字符串filtercolumn,串filtervalue)
{
this.FilterColumn = filtercolumn;
this.FilterValue = filtervalue;
this.FilterColumnCriteria ==;
}
公共字符串FilterColumn {搞定;组; }
公共字符串FilterValue {搞定;组; }
公共字符串FilterColumnCriteria {搞定;组; }
}
希望你不介意我张贴我的评论(这是有帮助的)作为一个答案......
如果您使用字符串化如下它应该工作...
JSON.stringify({FM:filterModel,TA:testparamA,TB:testparamA})
I am facing problem while posting multiple objects via ajax jquery to MVC 4 controller. It has been weeks but I can't seem to find a solution. I tried several approaches, sometimes the filterModel object is null and sometimes string parameters are null (doesn't matter even if I stringify of if I specify contentType or not)
What I want? I want to pass three objects: 1. filterModel 2.testparamA 3.testparamB What should I do to pass all three objects to MVC controller? What do I need to write in data: so I get all 3 object values?
The simplest Controller
[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"
});
}
}
The simplest View
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();
The simplest FilterModel class
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; }
}
Hope you don't mind me posting my comment (which was helpful) as an answer...
If you use stringify as follows it should work...
JSON.stringify({ fm: filterModel, ta: testparamA, tb: testparamA })
这篇关于AJAX后多数据来ASP.Net MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!