2013-05-02 73 views
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。

回答

3

它必须是这个,而不是;注意新数据名称:

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

该字段的名称需要与控制器中的名称相匹配,因此您必须使用alertTypeIds。

相关问题