2

I've got an ASP.NET MVC Razor application. In this application I have a MultiSelect control (which is basically a dropdown you can select multiple items from). I have subscribed to the "close" event of this control so that when it is closed, it passes a string of comma separated integers into the controller. However, although the method in the controller is being called, the value being passed into is always null. I have tested the event and I know that the string is being generated correctly. Here is the code in the event handler:

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

Here is the controller code:

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

Now I know that the string alertTypeIds is being generated correctly in the handler. I also know that the controller method is being hit. However, the value being passed into the controller (alertTypeIds) is always null. I also want to note that I am aware that this is sloppy code. I just wanted to make sure that I'm not passing in a null before I go through the work of writing code I may have to throw away.

1 Answer 1

3

It would have to be this instead; notice the new data name:

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

The name of the field needs to match the name in the controller, so you have to use alertTypeIds.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.