1

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!

1
  • Use HTML Form and serialize() instate of creating javascript object. Commented Feb 29, 2012 at 19:52

2 Answers 2

3

Change:

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

To:

$.post('@Url.Action("SendMessage")', {message: model}, function (data) {
    alert(data);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I never received e-mail notification of your answer. Problem resolved.
1

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.

1 Comment

Thank you! I never received e-mail notification of your answer. Problem resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.