最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Passing value from javascript to mvc controller - Stack Overflow

programmeradmin1浏览0评论

I am using tinyMCE (a rich text editor in js). Currently, I have a function as such:

   function GetEditorValue() {
        var val = tinyMCE.get('valueTextArea').getContent()

    }

which returns the text that was entered into the rich text editor. Now, is there a way to pass this data using POST to my mvc controller and access it there? (All this is being done in ASP.NET MVC 2 using C#)

I am using tinyMCE (a rich text editor in js). Currently, I have a function as such:

   function GetEditorValue() {
        var val = tinyMCE.get('valueTextArea').getContent()

    }

which returns the text that was entered into the rich text editor. Now, is there a way to pass this data using POST to my mvc controller and access it there? (All this is being done in ASP.NET MVC 2 using C#)

Share Improve this question asked Feb 4, 2011 at 10:53 snwrsnwr 751 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

You could send this value using AJAX. For example jQuery provides the .post() function:

var val = tinyMCE.get('valueTextArea').getContent();
$.post('<%= Url.Action("foo") %>', { value: val }, function(result) {
    // TODO: handle the success
    alert('the value was successfully sent to the server');
});

and inside your controller action:

[HttpPost]
public ActionResult Foo(string value)
{
    // Do something with the value
}

Now obviously because this is a RichText editor the value might contain dangerous characters and ASP.NET will reject them by throwing an exception. To avoid this you could decorate your controller action with the [ValidateInput(false)] attribute:

[HttpPost]
[ValidateInput(false)]
public ActionResult Foo(string value)
{
    // Do something with the value
}

and if you are using ASP.NET 4.0 you should also add the following to your web.config:

<httpRuntime requestValidationMode="2.0" />
发布评论

评论列表(0)

  1. 暂无评论