0

So here is my dilemma. I am making a rudimentary text area and i have some buttons to add bold, link, img, and italics.

When i don't insert any HTML in the text area, the method gets called properly and the breakpoint is hit. However, if I use HTML tags inside the text area the breakpoint doesn't even get called. through the following ajax controller:

@using (Ajax.BeginForm("CreatePost", "Accounts", new AjaxOptions
            {
                InsertionMode = InsertionMode.Replace,
                HttpMethod = "POST",
                LoadingElementId = "loading",
                UpdateTargetId = "CreateMessage"
            }))
                {
                    @Html.AntiForgeryToken()

When I use html it fails to even trigger the method. Is there a way to allow tags to be read normally and still be able to call the method?

underlying method:

[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
public async Task<ActionResult> CreatePost(BlogViewModel blog, string returnUrl)
{

    if (ModelState.IsValid)
    {
        try
        {
            await Task.Run(new Action( blog.CreatePost ) );
            //blog.CreatePost();
            if (blog.BlogID > 0)
            {
                ViewBag.Message = blog.Message;
            }
            else
            {
                ViewBag.Message = "Problem Posting new content";
            }
        }
        catch (Exception ex)
        {
            ViewBag.Message = "Problem Posting Content: '" + ex.Message;
        }
    }
    return PartialView("PopupMessage");
}//end CreatePost
EduardoFernandes
  • 3,091
  • 1
  • 13
  • 12
173901
  • 699
  • 1
  • 7
  • 29

1 Answers1

0

You need to allow HTML characters in your ajax method. To do so there are two solutions:

Solution 1:

Add the following attribute [ValidateInput(false)] in your method CreatePost(...). Below is the code:

[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
[ValidateInput(false)]
public async Task<ActionResult> CreatePost(BlogViewModel blog, string returnUrl)
{

    if (ModelState.IsValid)
    {
        try
        {
            await Task.Run(new Action( blog.CreatePost ) );
            //blog.CreatePost();
            if (blog.BlogID > 0)
            {
                ViewBag.Message = blog.Message;
            }
            else
            {
                ViewBag.Message = "Problem Posting new content";
            }
        }
        catch (Exception ex)
        {
            ViewBag.Message = "Problem Posting Content: '" + ex.Message;
        }
    }
    return PartialView("PopupMessage");
}//end CreatePost

Also, add the following tag in your web.config: <httpRuntime requestValidationMode="2.0" />. Here is the link with more details regarding httpRuntime.

Solution 2:

In your method CreatePost(...) simply add the attribute: [AllowHtml], as the code below:

using System.Web.Mvc;

......

[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
[AllowHtml]
public async Task<ActionResult> CreatePost(BlogViewModel blog, string returnUrl)
{

    if (ModelState.IsValid)
    {
        try
        {
            await Task.Run(new Action( blog.CreatePost ) );
            //blog.CreatePost();
            if (blog.BlogID > 0)
            {
                ViewBag.Message = blog.Message;
            }
            else
            {
                ViewBag.Message = "Problem Posting new content";
            }
        }
        catch (Exception ex)
        {
            ViewBag.Message = "Problem Posting Content: '" + ex.Message;
        }
    }
    return PartialView("PopupMessage");
}//end CreatePost

There is no need th change the web.config for this solution.

EduardoFernandes
  • 3,091
  • 1
  • 13
  • 12