jallen
jallen

Reputation: 612

MVC 3 Controller Action Not Accepting Values from JQuery .POST

Even though I can see my values posted in the http header, the controller keeps receiving the values as null and I am stumped after looking all over.

Here is my View:

$("#dialog-form").dialog({
            autoOpen: false,
            height: 360,
            width: 350,
            modal: true,
            buttons: {
                "Send Message": function () {
                    if ($('#name').val() == '' || $('#email').val() == '' || $('#message').val() == '') {
                        $('.validation_message').html('required');
                    }
                    else {
                        var model =
                        {
                            Name: $('#name').val(),
                            Email: $('#email').val(),
                            Message: $('#message').val(),
                            ProfileEmail: $('#profile_email').val()
                        };

                        $.post('@Url.Action("SendMessage")', model, function (data) {
                            alert(data);
                        });

                        e.preventDefault();
                        $(this).dialog("close");
                        $('.validation_message').hide();
                    }
                },
                "Cancel": function () {
                    $(this).dialog("close");
                    $('.validation_message').hide();
                }
            }
        });

        $("#button-contact")
            .button()
            .click(function () {
                $("#dialog-form").dialog("open");
            });

My Controller Action:

[HttpPost]
        [ValidateInput(false)]
        public ActionResult SendMessage(ProfileSendMessageModel message)
        {
            // some code here
                return Content("Your message has been sent!");

        }

The model always shows null but the elements in the header have values. Any help would be GREATLY appreciated!

Upvotes: 1

Views: 407

Answers (2)

jrummell
jrummell

Reputation: 43067

It's likely because your parameter is called message and ProfileSendMessageModel has a property named Message. The model binder is probably trying to bind ProfileSendMessageModel.Message instead of the whole model. Try changing the parameter name.

Upvotes: 1

Joe
Joe

Reputation: 82584

Change:

$.post('@Url.Action("SendMessage")', model, function (data) {
    alert(data);
});

To:

$.post('@Url.Action("SendMessage")', {message: model}, function (data) {
    alert(data);
});

Upvotes: 3

Related Questions