将Javascript对象数组传递给ASP.NET MVC3 Controller [英] Passing Javascript object array to ASP.NET MVC3 Controller
问题描述
i有一个javascript函数用于调用控制器函数'GetTemp',如下所示
函数test()
{
var _TempIds = {};
_TempIds [iHospitalId] =1;
_TempIds [iClinicId] =2;
_TempIds [iDoctorId] =3;
$ .ajax({
类型:POST,
url:../ Home / GetTemp,
contentType:application / json; charset = utf-8,
dataType:json,
数据:JSON.stringify(_TempIds),
错误:函数(xhr,statusText){
alert(错误处理请求时,请再试一次。);
},
成功:函数(结果,textStatus){
if(textStatus ==success){
alert(succ);
}
}
});
}
但是,执行chrome中的错误部分。但它在ie.getting错误中工作正常>
数据:JSON.stringify(_TempIds)。如何解决这个问题?
谢谢..
.ajax({
type :POST,
url:../ Home / GetTemp,
contentType:application / json; charset = utf-8,
dataType:json,
data:JSON.stringify(_TempIds),
error:function(xhr,statusText){
alert(处理请求时出错,请再试一次。);
},
成功:函数(结果,textStatus){
if(textStatus ==success){
alert(succ);
}
}
});
}
但是,执行chrome中的错误部分。但它在ie.getting错误中工作正常>
数据:JSON.stringify(_TempIds)。怎么解决这个?
谢谢..
如果我理解你的问题,你想把一个数组传递给你控制器所以你可以这样做
< script>
var _TempIds = [];
_TempIds [ HospitalId] = 1;
_TempIds [ ClinicId] = 2;
_TempIds [ DoctorId] = 3;
function fun()
{
.ajax({
类型: POST,
cache: false ,
url: / Controller / Action ,
dataType: json,
traditional: true ,
data:{
_TempIds:_TempIds
}
});
}
< / script>
在您的控制器中可以像这样访问你的数组
public ActionResult Index(IEnumerable< string> _TempIds)
{
// 在这里使用你的逻辑
return View();
}
< pre lang = text>我希望这个可以帮助你
干杯!!
Hi,
i have a javascript function for calling the controller function 'GetTemp' as below
function test() { var _TempIds = {}; _TempIds["iHospitalId"] = "1"; _TempIds["iClinicId"] = "2"; _TempIds["iDoctorId"] = "3"; $.ajax({ type: "POST", url: "../Home/GetTemp", contentType: "application/json; charset=utf-8", dataType: "json", data: JSON.stringify(_TempIds), error: function (xhr, statusText) { alert("Error while processing the request, please try again."); }, success: function (result, textStatus) { if (textStatus == "success") { alert("succ"); } } }); }
But, executing the error part in chrome.but it working fine in ie.getting error at
data: JSON.stringify(_TempIds). how to solve this?
Thanks..
.ajax({ type: "POST", url: "../Home/GetTemp", contentType: "application/json; charset=utf-8", dataType: "json", data: JSON.stringify(_TempIds), error: function (xhr, statusText) { alert("Error while processing the request, please try again."); }, success: function (result, textStatus) { if (textStatus == "success") { alert("succ"); } } }); }
But, executing the error part in chrome.but it working fine in ie.getting error at
data: JSON.stringify(_TempIds). how to solve this?
Thanks..
If i understand your question right , you want to pass an array to your controller so you can do like this
<script> var _TempIds = []; _TempIds["HospitalId"] = "1"; _TempIds["ClinicId"] = "2"; _TempIds["DoctorId"] = "3"; function fun() {
.ajax({ type: "POST", cache: false, url: "/Controller/Action", dataType: "json", traditional: true, data: { _TempIds: _TempIds } }); } </script>
and in your controller you can access your array like this
public ActionResult Index(IEnumerable<string>_TempIds) { // use your logic here return View(); } <pre lang="text">I hope this will help you Cheers!!
这篇关于将Javascript对象数组传递给ASP.NET MVC3 Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!