[英]Passing data from javascript to MVC Controller with multiple parameters
我有Controller
OrderAssignmentRuleSet
和以下ActionResult
方法
public ActionResult OrderAssignmentRuleSetEdit(string customerId, string Name= null, List<string> listOfItems = null)
{
}
下面是我将数据传递给我上面的controller
方法的Javascript
$("#rolesValues").change(function () {
var id ='0001'
var name = 'admin'
var listOfItems= [];
//Populating listofItems with multiselect dropdown
if ($('#ddlItemsList option:selected').length > 0) {
listOfItems = $.map($('#ddlItemsList option:selected'), function (item) {
return item.value;
});
}
var data = {
customerId: id,
Name: name,
listOfItems: listOfItems
}
$.ajax({
type: 'POST',
url: '/OrderAssignmentRuleSet/OrderAssignmentRuleSetEdit',
traditional : true,
data: data,
content: "application/json;",
dataType: "json",
success: function () {
}
});
我的问题是将两个strings
(id 和 name)和一个array
(listofItems 作为列表)传递给controller
,当前代码不返回任何内容。 请帮忙看看这段代码有什么问题?
您正尝试以POST
方式发送您发布的数据。 但是您正尝试在操作方法中的查询参数中收集这些数据。
所以尝试创建一个类
public class Sample
{
public string customerId { get; set; }
public string Name { get; set; }
public List<string> listOfItems { get; set; }
}
然后修改你的动作方法
public ActionResult OrderAssignmentRuleSetEdit([FromBody] Sample sample)
{
//Your stuff here
}
你可以用这种方式解决你的问题。
myCustomFunction = function () {
var model = {
customerId: '',
Name: '',
listOfItems: ''
};
var url = '/OrderAssignmentRuleSet/OrderAssignmentRuleSetEdit';
model.customerId = $("#customerId").val();// get customer id.
model.Name = $("#Name").val();// get name;
model.listOfItems = [];//get value of list;
$.ajax({
url: url,
type: "Post",
data: JSON.stringify(model),
dataType: "json",
contentType: "application/json"
}).done(function (response) {
console.log(response);
}).fail(function (response) {
console.log(response);
});
},
//在服务器端获取数据,将模型作为客户端要求。
[HttpPost]
public virtual JsonResult OrderAssignmentRuleSetEdit(MyCustomModel model)
{
try
{
ValidationViewModel msg = new ValidationViewModel();
return Json(new { success = msg.Result, message = msg.Message }, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json(new { success = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
}
}
//Javascript method
function postData(parsData)
{
var dataToSend = { ParsData: parsData};
var ReceivedData= JSON.stringify( dataToSend );
$.ajax({
url: '@Url.Action("SetData")',
type: 'POST',
data: ReceivedData
}).done(function (response)
{
console.log(response);
document.getElementById("result").innerHTML = response.resultHint;
document.getElementById("amount").innerHTML = response.resultAmount;
document.getElementById("image").src = response.imageUrl;
});
}
//Javascript method Implementation
postData("Fuking code");
//C# Controller
public class ReceivedData
{
public string ParsData{ get; set; }
}
public class Result
{
public string resultHint { get; set; }
public string resultAmount { get; set; }
public string imageUrl { get; set; }
public string decoded { get; set; }
}
[HttpPost]
public ActionResult SetData(string receivedData)
{
//var jss = new JavaScriptSerializer();
// ReceivedData decodedQR = new JavaScriptSerializer().Deserialize<ReceivedData>(receivedData);
// var dataObject = new JavaScriptSerializer().Deserialize(receivedData);
// .. do something with data object
var jsonFromRequest = new System.IO.StreamReader(Request.InputStream).ReadToEnd();
ReceivedData decodedQR = Newtonsoft.Json.JsonConvert.DeserializeObject<ReceivedData>(jsonFromRequest);
Result result= new Result();
result.resultHint = "Tem certeza que pretende permitir que o João Deposite o valor de ";
result.decoded = decodedQR.ParsData;
result.resultAmount = "200 MT";
result.imageUrl = "https://picsum.photos/id/237/200/300";
return Json(result);
}
Credit to:
https Credit to:
//mestanzasoft.wordpress.com/2018/03/05/pass-data-from-asp-net-mvc-view-to-controller-with-ajax-and-json/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.