jQuery 如何将多个参数从ajax传递给mvc控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28316378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass multiple parameters from ajax to mvc controller?
提问by Rasmita Dash
Controller
控制器
[HttpPost]
public ActionResult Save(string StrContactDetails, bool IsPrimary)
{
}
$.ajax({
async: true,
type: "POST",
url: @url.Action("Helper","Save"),
data: {
StrContactDetails: Details,
IsPrimary: true
},
//data: "StrContactDetails=" + Details + "&IsPrimary=" + true,
//data: "{StrContactDetails:'" + Details + "',IsPrimary:"+ true + "}",
//contentType: "application/json; charset=utf-8",
success: function() {
},
error: function() {
}
});
This works when my action method expects a single parameter and I pass the single parameter from ajax. But, I am unable to call the action with two parameters when it expects two parameters. So, there is some issue in passing parameters. May be content Type.
当我的 action 方法需要单个参数并且我从 ajax 传递单个参数时,这会起作用。但是,当需要两个参数时,我无法使用两个参数调用该操作。因此,传递参数存在一些问题。可能是内容类型。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Default", action = "Login", id = UrlParameter.Optional }
).DataTokens = new RouteValueDictionary(new { area = "MyArea" });
I can call .../TestProj/MyArea/Helper/Save/StrContactDetails="Test"
when my action method is as follows.
.../TestProj/MyArea/Helper/Save/StrContactDetails="Test"
当我的操作方法如下时,我可以调用。
public ActionResult Save(string StrContactDetails)
{
return Content("called");
}
I can call .../TestProj/MyArea/Helper/SaveEmergencyContact/StrContactDetails="test"?IsPrimary=true
if my action method is as follows. But I am getting 404 for .../TestProj/MyArea/Helper/SaveEmergencyContact/StrContactDetails="test"/IsPrimary=true
(replace ? with /)
.../TestProj/MyArea/Helper/SaveEmergencyContact/StrContactDetails="test"?IsPrimary=true
如果我的操作方法如下,我可以调用。但是我得到了 404 .../TestProj/MyArea/Helper/SaveEmergencyContact/StrContactDetails="test"/IsPrimary=true
(用 / 替换?)
public ActionResult Save(string StrContactDetails, bool IsPrimary)
{
return Content("called");
}
What I am missing here? Do I need to make route config change for ajax call with 2 parameters?
我在这里缺少什么?我是否需要使用 2 个参数更改 ajax 调用的路由配置?
回答by vinayan
I think you may need to stringify the data using JSON.stringify.
我认为您可能需要使用 JSON.stringify 对数据进行字符串化。
var data = JSON.stringify({
'StrContactDetails': Details,
'IsPrimary':true
});
$.ajax({
type: "POST",
url: @url.Action("Dhp","SaveEmergencyContact"),
data: data,
success: function(){},
contentType: 'application/json'
});
So the controller method would look like,
所以控制器方法看起来像,
public ActionResult SaveEmergencyContact(string StrContactDetails, bool IsPrimary)
回答by Rush.2707
You can do it by not initializing url and writing it at hardcode like this
您可以通过不初始化 url 并像这样在硬编码中编写它来实现
//var url = '@Url.Action("ActionName", "Controller");
$.post("/Controller/ActionName?para1=" + data + "¶2=" + data2, function (result) {
$("#" + data).html(result);
............. Your code
});
While your controller side code must be like this below:
虽然您的控制器端代码必须如下所示:
public ActionResult ActionName(string para1, string para2)
{
Your Code .......
}
this was simple way. now we can do pass multiple data by json also like this:
这是简单的方法。现在我们也可以像这样通过 json 传递多个数据:
var val1= $('#btn1').val();
var val2= $('#btn2').val();
$.ajax({
type: "GET",
url: '@Url.Action("Actionre", "Contr")',
contentType: "application/json; charset=utf-8",
data: { 'para1': val1, 'para2': val2 },
dataType: "json",
success: function (cities) {
ur code.....
}
});
While your controller side code will be same:
虽然您的控制器端代码将相同:
public ActionResult ActionName(string para1, string para2)
{
Your Code .......
}
回答by malkam
Try this:
尝试这个:
var req={StrContactDetails:'data',IsPrimary:'True'}
$.ajax({
type: 'POST',
data: req,
url: '@url.Action("SaveEmergencyContact","Dhp")',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(req),
success: function (data) {
alert("Success");
},
error: function (ob, errStr) {
alert("An error occured.Please try after sometime.");
}
});
回答by raju ranjan
function toggleCheck(employeeId) {
var data = 'referenceid= '+ employeeId +'&referencestatus=' 1;
console.log(data);
$.ajax({
type : 'POST',
url : 'edit',
data : data,
cache: false,
async : false,
success : function(employeeId) {
alert("Success");
window.redirect = "Users.jsp";
}
});
}
回答by raju soneri
var data = JSON.stringify
({
'StrContactDetails': Details,
'IsPrimary': true
})