Pascal Mormin
Pascal Mormin

Reputation: 216

Asp.net MVC Text Editor pass long html code to controller

I wants to make a text editor for a site to allow user to write his own text or past text from another source to create pages (like wordpress for example). To do that i chose Jquery-Te as text editor.

My problem is that when i copy/paste a text from wikipedia for example, i got a 404.15 error. I have read that i can do some changes on the RequestLimit but i can't know how long user text can be. I can set a limit to 400000 but if user enter 500000 he will get an error. Is there another way to allow user to pass a lot of text?

My second problem is Jquery-Te generates html and my text is in fact an html code. Sometimes i cant get error like "A potentially dangerous Request.Form value was detected from the client".

Can someone help me to do what i want?

I give you my code :

View :

<form action="SaveArticle" method="post">
    @Html.TextBoxFor(x => x.Article.Titre)
    @Html.TextBoxFor(x => x.Article.Contenu, new { @class = "editor" })
    <button type="submit" class="btn btn-info btn-circle">
        <i class="fa fa-search"></i>
    </button>
</form>


<script>
    $(".editor").jqte();
</script>

Controller :

public ActionResult GestionDesPages(GestionDesPagesViewModel gdpvm)
        {
            return View(gdpvm);
        }

        [HttpPost]
        public ActionResult SaveArticle(GestionDesPagesViewModel gdpvm)
        {
            Articles article = gdpvm.Article;
            article.Date = DateTime.Now;
            article.Auteur = "Pascal";

            db.Articles.Add(article);
            db.SaveChanges();

            return View("GestionDesPages");
        }

ViewModel :

public class GestionDesPagesViewModel
    {
        public Articles Article{get; set;}
    }

Model :

public partial class Articles
{
    public int Id { get; set; }
    public string Titre { get; set; }
    public string Contenu { get; set; }
    public string Auteur { get; set; }
    public Nullable<System.DateTime> Date { get; set; }
    public Nullable<bool> Actif { get; set; }
}

Sorry For Bad English. Thanks.

EDIT :

Thank You Nava. The solution given by Nava helped me to resolve all my problems. I didn't said that i uses EntityFramework. To add [AllowHttp], I used the procedure defined in this theAdd annotation to Entity Framework Generated class

Upvotes: 1

Views: 2514

Answers (1)

Andy T
Andy T

Reputation: 9881

To allow HTML to be inputted in your form and not get the "potentially dangerous" message add the [AllowHtml] attribute to Contenu

[AllowHtml]
public string Contenu { get; set; }

Keep in mind that when you display that field, you will probably want to Html encode to prevent a Cross site scripting problem

Upvotes: 1

Related Questions