如何将多个参数传递到MVC控制器,包括字符串列表/字符串数组? [英] How to pass multiple parameters to MVC controller including list/array of strings?
本文介绍了如何将多个参数传递到MVC控制器,包括字符串列表/字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个ajax调用,我正在尝试将字符串数组以及id和search参数发布到控制器.有人能告诉我该怎么做吗? 示例:
I have a ajax call and I'm trying to post an array of strings, as well as an id and search parameter to a controller. Anyone able to show me how to do this? Example:
jQuery
jQuery
$("body").on("click", "#btnClientModalSearch", function (e) {
preventDefaultAction(e);
var url = GetUrlPath() + "/Client/GetClientCalendarSearchResults";
var searchTypeId = $("#ddlSearchType").val();
var searchParameter = $("#tbSearchParameter").val();
var diaryId = $("#SelectedEventId").val();
var values = [];
$(".referralIdList").each(function () {
var referralId = $(this).attr("id");
var arr = referralId.split('referralId');
values.push(arr[1]);
});
var postData = { alreadyAddedReferralIds: values };
$.ajax({
url: url,
data: { searchTypeId: searchTypeId, searchParameter: searchParameter, diaryId: diaryId, alreadyAddedReferralIds: postData },
cache: false,
type: "POST",
success: function (result) {
if (result.success === true) {
$("#searchResultsPlaceHolder").html(result.view);
}
},
error: function (responseText, textStatus, errorThrown) {
alert('Error - ' + errorThrown);
}
});
});
控制器
Controller
public JsonResult GetClientCalendarSearchResults(string searchTypeId, string searchParameter, string diaryId, List < string > alreadyAddedReferralIds) {}
当我尝试此操作时, alreadyAddedReferralIds 在控制器上始终为空.
When I try this, the alreadyAddedReferralIds is always empty at the controller.
推荐答案
尝试用单个对象参数替换控制器中的参数.所以你会有类似的东西:
Try replacing the parameters in the controller with a single object parameter. So you would have something like:
public JsonResult GetClientCalendarSearchResults(SearchResultsRequest request)
public class SearchResultRequest
{
public string searchTypeId { get; set; }
public string searchParameter { get; set; }
public string diaryId { get; set; }
public List<string> alreadyAddedReferralIds { get; set; }
}
从我的经验发布到需要多个参数的操作方法,不会填充其值.
From my experience posting to an action method that requires multiple parameters won't have their values populated.
这篇关于如何将多个参数传递到MVC控制器,包括字符串列表/字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文