Jose3d
Jose3d

Reputation: 9277

MVC3 CKEditor and Ajax issue

I have the following action/controller on MVC3:

    [HttpPost]
    public ActionResult AX_AddItemResponse(ItemResponsesVM response)
    {
        return View(response);
    }

This is the viewmodel that im using:

public class ItemResponsesVM
    {
        [Display(Name = "Message")]
        [Required(ErrorMessage = "Message is required")]
        [StringLength(250, ErrorMessage = "Please add a maximum of 250 chars")]
        public string Message
        { get; set; }
     }

For other hand i have this ajax call that should go to the action described above:

$(document).ready(function () {
    var form=$("#MyForm"),
    $("#Submitbutton").click(function () {

        $('#Message').each(function () {
            var $textarea = $(this);
            $textarea.val(CKEDITOR.instances[$textarea.attr('name')].getClearText());
        })
        ,
        $.ajax({
            type: "POST",
            url: "@(Url.Action("AX_AddItemResponse","Responses"))",
            data: form.serialize(),
            success: function () {
                alert("AllGood");
            }
            ,
            error:function()
            {
                alert("AllBad");
            }
        });
    });

});

Im using the CKEditor to enhance the textarea with id="Message" element, the problem is that i dont know how to serialize the form in order to reach the action described with an instance of ItemResponsesVM.

Upvotes: 0

Views: 776

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

You are already correctly serializing the form:

data: form.serialize()

Just make sure you decorate your Message property with the [AllowHtml] attribute or the ASP.NET runtime might reject the request if it contains dangerous characters such as <, >, ...:

public class ItemResponsesVM
{
    [Display(Name = "Message")]
    [Required(ErrorMessage = "Message is required")]
    [StringLength(250, ErrorMessage = "Please add a maximum of 250 chars")]
    [AllowHtml]
    public string Message { get; set; }
}

Also there seems to be some comma (,) before the $.ajax call which should be a semi-colon (;).

Upvotes: 2

Related Questions