gpt4 book ai didi

asp.net-mvc - 在 ASP.NET MVC Razor 中将字符串从 View 传递到 Controller

转载 作者:行者123 更新时间:2023-12-04 04:59:18 24 4
gpt4 key购买 nike

我有一个 ASP.NET MVC Razor 应用程序。在这个应用程序中,我有一个 MultiSelect 控件(它基本上是一个下拉列表,您可以从中选择多个项目)。我已经订阅了这个控件的“关闭”事件,这样当它关闭时,它会将一串逗号分隔的整数传递给 Controller ​​。但是,虽然调用了 Controller 中的方法,但传入的值始终为空。我已经测试了该事件,并且我知道正在正确生成字符串。这是事件处理程序中的代码:

    `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");
});
};`

这是 Controller 代码:
`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("anorkin@daymon.com");
int associateId = user[0].AssociateId;

subscriptionRepository.UpdateSubscriptionForUser(associateId, isSubscribedToNewItem, isSubscribedToNewCustomer, isSubscribedToNewSupplier,
isSubscribedToBmTerminated, isSubscribedToBmChange, isSubscribedToItemCategoryChange);
}`

现在我知道在处理程序中正确生成了字符串 alertTypeIds。我也知道 Controller 方法正在被击中。但是,传递给 Controller ​​的值 (alertTypeIds) 始终为 null。我还想指出,我知道这是草率的代码。我只是想确保在我完成编写可能不得不扔掉的代码的工作之前我没有传入空值。

最佳答案

应该是这个;注意新的数据名称:

 $.post('@Url.Action("SubscribeToAlerts")', { alertTypeIds: alertTypeIds }, 
function (result) {
alert("the value was successfully sent to the server");
});

字段的名称需要与 Controller 中的名称匹配,因此必须使用alertTypeIds。

关于asp.net-mvc - 在 ASP.NET MVC Razor 中将字符串从 View 传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16344192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com