0

How do I grab the data in my textarea and stuff it in my model?

I'm in a Create() View, and I want to access the model, to put the contents of a

<textarea>

into one of the model's properties, the Content property in this case.

namespace TestTinyMCE.Models {
  public class TestBlog {
    public int TestBlogId { get; set; }
    public string Title { get; set; }
    public DateTime PostedOn { get; set; }
    public string Tags { get; set; }

    public string Content { get; set; }
  }
}

I can't use TextAreaFor as I'm accepting HTML markup (bold, italic, and so on). I'm using TinyMCE on my textarea if that matters.

I tried hooking the submit event via JQuery's .submit API:

<script type="text/javascript">
  $(document).ready(function () {
    tinyMCE.init({
      theme: "advanced",
      mode: "textareas"
    });

    $('#contentEditor').submit(function () {
      alert('Handler for .submit() called.');
      return false;
    });
  });
</script>

but while the .submit() occurred in $(document).ready, the handler itself never fires off.

and here's my textarea:

<div class="editor-field">
  <textarea id="contentEditor" name="contentEditor"></textarea>
</div>
3
  • Model binding uses normal HTTP form parameters. You don't need to do anything. Commented Mar 25, 2012 at 23:01
  • This is a classic example of the XY problem. meta.stackexchange.com/questions/66377/what-is-the-xy-problem Commented Mar 25, 2012 at 23:15
  • It is very important that you understand the basics of HTML forms and HTTP requests to work with MVC. (understanding the basics of MVC model binding and validation is also helpful) Commented Mar 25, 2012 at 23:16

1 Answer 1

1

I can't use TextAreaFor as I'm accepting HTML markup (bold, italic, and so on). I'm using TinyMCE on my textarea if that matters.

Wrong.

TextAreaFor() emits a normal <textarea> just like your; you can still hook up TinyMCE to it.


Your actual problem is that <textarea>s don't fire submit events.
You need to handle the <form>'s submit event.

However, you don't actually need to do that at all.

Sign up to request clarification or add additional context in comments.

5 Comments

TextAreaFor() will bring up the TinyMCE editor. However, once the form is submitted, you get the "Server Error in '/' Application. A potentially dangerous Request.Form value was detected from the client (Content="<h1>Hello World!</h1..."). " error. I don't want to turn off the rest of requestValidationMode just for this one textarea.
@JohnW.S.Marvin: That's a server-side feature; it has nothing to do with TextAreaFor(). You need to add [AllowHtml] to the posted model property.
How do I add a Form id to a form created with Html.BeginForm?
In the htmlAttributes parameter. However, you don't need to do any of this, and it won't help anyway.
Thank you so much, [AllowHtml] is just what I needed.

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.