Summernote and form submission in MVC c#
Asked Answered
A

3

11

I am using the summernote plugin for text box: http://summernote.org/#/getting-started#basic-api

This is the form I have using summmernote:

<div class="modal-body" style="max-height: 600px">
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        <fieldset class="form-horizontal">
            <div id="textForLabelLanguage"></div>
            <button type="submit" class="btn btn-primary">Save changes</button>
            @Html.ActionLink("Cancel", "Index", null, new { @class = "btn " })
        </fieldset>
    }
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $('#textForLabelLanguage').summernote();
    });
</script>

Now, In my controller, this is the code I have:

public ActionResult Create(UserInfo newInfo , [Bind(Prefix = "textForLabelLanguage")] string textForLabelLanguage)
{
    //logic here
}

Now the problem is that textForLabelLanguage param is always null.

This happens because I have to pass $('#textForLabelLanguage').code(); into MVC when submiting the form but I have no idea how to do that!

How do I solve my problem?

Alcestis answered 9/4, 2015 at 13:54 Comment(6)
where is your form element that will have the html?Cowbell
It is right before the javascript. That is teh HTML using mvc forms generation.Alcestis
I mean the input field?Cowbell
@DanielA.White: ` <div id="textForLabelLanguage"></div>`. This is it. And then the JS does the rest. It is all in the summernote documentation :DAlcestis
That isn't an input fieldCowbell
@DanielA.White: I never said it was :P Tricky isn't it? You just assumed it !Alcestis
A
15

I found my solution to the problem. This is how I am making the controller get the correct information:

<div class="modal-body" style="max-height: 600px">
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        <fieldset class="form-horizontal">
            <textarea name="textForLabelLanguage" id="textForLabelLanguage" />
            <button type="submit" class="btn btn-primary">Save changes</button>
            @Html.ActionLink("Cancel", "Index", null, new { @class = "btn " })
        </fieldset>
    }
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $('#textForLabelLanguage').summernote();
    });
</script>

Basically, if I use a textarea with a name instead of an input or anything else, it works!

However, and be warned, even though this solution works, I then get a error in the controller saying:

A potentially dangerous Request.Form value was detected from the client

This happens because I am allowing HTML. But this is a problem for another question!

Alcestis answered 10/4, 2015 at 13:21 Comment(1)
FYI [ValidateInput(false)] allows HTML to passed to a controller actionRabblerouser
W
8

Please, use [AllowHTML]

There's a good article on MSDN Request Validation in ASP.NET

"To disable request validation for a specific property, mark the property definition with the AllowHtml attribute:"

[AllowHtml]
public string Prop1 { get;  set; }
Wolfram answered 12/2, 2017 at 1:38 Comment(0)
W
3

similar to what was posted earlier you can use the HTML helper

@HTML.TextAreaFor( m=> m.text, new {@id = "textFor*Model*"})

instead of <textarea>

Willard answered 7/6, 2016 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.