2
我有一个ASP.NET MVC Razor应用程序。在这个应用程序中,我有一个MultiSelect控件(基本上可以从下拉列表中选择多个项目)。我已经订阅了这个控件的“close”事件,所以当它关闭时,它将一串逗号分隔的整数传递给控制器。但是,虽然控制器中的方法正在被调用,但传入的值始终为空。我测试了这个事件,并且我知道该字符串正在正确生成。这是在事件处理程序的代码:在ASP.NET MVC中将视图中的字符串传递给控制器Razor
`function close() {
var alertTypeIds = $("#alertMultiSelect").val().toString();
//alert("this is the alertTypeId: " + alertTypeIds);
$.post('@Url.Action("SubscribeToAlerts")', { value: alertTypeIds }, function (result) {
alert("the value was successfully sent to the server");
});
};`
这里是控制器代码:
`public void SubscribeToAlerts(string alertTypeIds)
{
bool isSubscribedToNewItem = false;
bool isSubscribedToNewCustomer = false;
bool isSubscribedToNewSupplier = false;
if (alertTypeIds.Contains('1')){
isSubscribedToNewItem = true;
}
if (alertTypeIds.Contains('2')) {
isSubscribedToNewCustomer = true;
}
if (alertTypeIds.Contains('3')) {
isSubscribedToNewSupplier = true;
}
var subscriptionRepository = new BMTool.Persistance.SubscriptionRepository();
var userRepository = new BMTool.Persistance.UserRepository();
IList<BMTool.Models.User> user = userRepository.GetUser("[email protected]");
int associateId = user[0].AssociateId;
subscriptionRepository.UpdateSubscriptionForUser(associateId, isSubscribedToNewItem, isSubscribedToNewCustomer, isSubscribedToNewSupplier,
isSubscribedToBmTerminated, isSubscribedToBmChange, isSubscribedToItemCategoryChange);
}`
现在我知道,被在处理程序正确生成字符串alertTypeIds。我也知道控制器方法正在被打击。但是,传递给控制器的值(alertTypeIds)始终为空。我也想注意到,我意识到这是潦草的代码。我只是想确保在我完成编写我可能不得不扔掉的代码的工作之前,我没有通过null。