【问题标题】:Textarea wont function when contains html [duplicate]包含html时,Textarea不会起作用[重复]
【发布时间】:2014-09-20 18:41:20
【问题描述】:

所以这是我的困境。我正在制作一个基本的文本区域,并且我有一些按钮可以添加粗体、链接、img 和斜体。

当我没有在文本区域中插入任何 HTML 时,该方法会被正确调用并会命中断点。但是,如果我在文本区域内使用 HTML 标记,甚至不会调用断点。 通过以下 ajax 控制器:

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

当我使用 html 时,它甚至无法触发该方法。有没有办法让标签被正常读取,仍然可以调用方法?

底层方法:

[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

【问题讨论】:

    标签: c# html ajax razor asp.net-mvc-5


    【解决方案1】:

    您需要在 ajax 方法中允许 HTML 字符。为此,有两种解决方案:

    解决方案 1:

    在您的方法CreatePost(...) 中添加以下属性[ValidateInput(false)]。下面是代码:

    [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
    

    另外,在您的 web.config 中添加以下标签:&lt;httpRuntime requestValidationMode="2.0" /&gt;。这是link,其中包含有关 httpRuntime 的更多详细信息。

    解决方案 2:

    在您的方法CreatePost(...) 中只需添加属性:[AllowHtml],如下代码:

    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
    

    此解决方案无需更改 web.config。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 2012-09-22
    • 1970-01-01
    • 2013-10-30
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多