Skip to content Skip to sidebar Skip to footer

How Do I Post A Javascript Variable To An Asp.net Mvc Model?

Im trying to use the Yahoo rich text editor in my web application. I'm kind of new to web programming so this may be a silly question. I'm using a custom model called 'blogpost'.

Solution 1:

You can't do this in your MVC View. You need to do it in javascript.

You'll need to hook the Submit event on the form and then get the value of the text in the editor, and add it to the post data. Something like:

$('form').submit(function(event){
    // cancel the default action
    event.preventDefault();
    var body = escape(myEditor.get('element').value);

    var theForm = $(this);
    $.post(theForm.attr('action'), theForm.serialize() + '&body=' + body, function (data) {
        // do whatever with the result
    });
});

Another way to do it would be to add a hidden field to your form and update that field with the value of the editor:

<inputid="body" name="body"type="hidden" value=""/>

then instead of setting the body variable you can do this:

YAHOO.util.Event.on('Create', 'click', function () {
    myEditor.saveHTML();
    $('#body').attr('value', myEditor.get('element').value);
}); 

Then the data is in the form and the form will handle the rest.

Post a Comment for "How Do I Post A Javascript Variable To An Asp.net Mvc Model?"