【问题标题】:"Required" attribute not working when "AllowHtml" attribute is used. MVC 5使用“AllowHtml”属性时,“必需”属性不起作用。 MVC 5
【发布时间】:2015-12-10 14:40:01
【问题描述】:

我想用所见即所得的编辑器发布一个 html,所以我在我的属性上使用了 [AllowHtml] 属性。它可以工作,但是当我将它与[Required][StringLength] 属性一起使用时,它会停止工作。 ModelState.IsValid 即使 prop 为空也返回 true。

是的,我知道如果它为空我可以手动控制它并向ModelState添加错误,但是为什么呢?

为什么会这样?有没有更好的方法将一些 HTML 代码发布到后端?

我的 dto:

public int Id { get; set; }

[Required(ErrorMessage = CErr.RequiredField)]
[StringLength(100, ErrorMessage = CErr.Min5Max100)]
[MinLength(5, ErrorMessage = CErr.Min5Max100)]
public string Name { get; set; }

[AllowHtml]
[Required(ErrorMessage = CErr.RequiredField)]
[StringLength(4000, ErrorMessage = CErr.TooLongValue)]
public string HtmlBody { get; set; }

我的行动:

[Route("new")]
[HttpPost]
public ActionResult NewMessageLayout(ManageMessageLayoutDto model)
{
    if (ModelState.IsValid)
    {
        var response = Repositories.MessageLayoutRepository.SaveMessageLayout(model, CurrentUser.Id);

        if (response.Status == ResultStatus.Success)
        {
            return RedirectToAction("MessageManagement");
        }

        else
        {
            ModelState.AddModelError("error", response.Message);
            return View("ManageMessageLayout", model);
        }
    }

    return View("ManageMessageLayout", model);
}

还有一些 HTML:

@using (Html.BeginForm())
{
    @Html.HiddenFor(m => m.Id)

    <label>Name <span class="req">*</span></label>
    @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
    @Html.ValidationMessageFor(m => m.Name, null, new { @class = "label label-danger" })

    <label>Content <span class="req">*</span></label>
    @Html.TextBoxFor(m => m.HtmlBody)
    @Html.ValidationMessageFor(m => m.HtmlBody, null, new { @class = "label label-danger" })

    <input class="btn btn-success btn-block btn-lg" type="submit" value="@(editing ? "Save" : "Add")" />
}

【问题讨论】:

  • 您是否在使用任何富文本编辑器,例如 tiny mce 或 Ckeditor?
  • @MDDDC 是的,我正在使用 Ckeditor,但我尝试使用常规文本框和区域,但它是相同的......
  • 您是否获得有关 HTmlBody 的 post 方法的任何数据?您能停下来看看吗?
  • @MDDDC 我的朋友,这似乎是无效的。
  • @gkon,“我的朋友,这似乎是无效的。”你是什​​么意思?它要么为空,要么不为空。你设置断点了吗?

标签: asp.net-mvc validation asp.net-mvc-5


【解决方案1】:

我无法复制您的错误。不过,我确实注意到了一些事情。首先,根据 ckEditor 文档,textbox 不是编辑器的有效附加点。你应该使用textarea

CkEditor Developer Documentation

此时任何 textarea、p 或 div 元素都可以转换为 使用 ckeditor() 方法的富文本编辑器。

接下来,请注意我在[Required( 属性中添加了AllowEmptyStrings=false。这可能是您缺少的重要部分(将在没有测试的情况下进行测试)- 测试似乎表明不设置 AllowEmptyStrings 不会影响此测试设置的结果,如 default value for this property is false。模型仍然无效。

设置

  1. VS 2015,MVC 5.2.2,.NET 4.5.1,jQuery 1.10.2,ckEditor 3.6.4

查看模型

public class TestViewModel
{
    [AllowHtml]
    [Required(AllowEmptyStrings = false, ErrorMessage = "This should be filled out")]
    [StringLength(4000, ErrorMessage="Its too big")]
    public string HtmlBody { get; set; } 
}

控制器操作

    public ActionResult About()
    {
        return View(new TestViewModel());
    }

    [HttpPost]
    public ActionResult About(TestViewModel model)
    {
        if (!ModelState.IsValid) throw new Exception();
        var test = model.HtmlBody;
        return RedirectToAction("Contact");
    }

查看

@model WebApplication6.Models.TestViewModel

@{
    ViewBag.Title = "About";
}
<h2>Test of HTML</h2>

@using (Html.BeginForm())
{
    <label>Content <span class="req">*</span></label>
    @Html.TextAreaFor(m => m.HtmlBody)
    @Html.ValidationMessageFor(m => m.HtmlBody, null, new { @class = "label label-danger" })
    <input type ="submit" value="test on server"/>
}

@section scripts
{
    <script type="text/javascript">
    $(function () {
        $('#HtmlBody').ckeditor();
    });
</script>
}

结果: 基本上是因为模型状态无效而抛出异常

【讨论】:

  • 谢谢汤米。我会试试 AllowEmptyStrings。而且,我知道文本框不适用于 ckeditor,但我在此之前使用过,我将其更改为文本框只是为了测试。
  • @gkon - 我的测试表明它没有任何区别,它应该默认为 false。
  • 'AllowEmptyStrings' 也不起作用。我也创建了另一个项目,并得到了像你一样的错误。我尝试不使用属性路由、验证属性的每种组合以及许多不同的发布数据,但它是相同的。 'AllowHtml' 阻止所有其他属性起作用。
  • @gkon - 它只会阻止它在您的一个项目中工作。该控件/属性还有其他事情发生。例如,确保 ckEditor 没有通过空标签 &lt;p/&gt; 发送。这就是这里的任何人都可以帮助您,直到我们可以推断出您的配置与开箱即用实施的不同之处。
【解决方案2】:

我发现了问题。

我的表示层使用的是 5.2.2.0 版本的 System.Web.Mvc,但存储库层使用的是 5.2.3.0。我将其降级为 5.2.2.0。现在它可以正常工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    相关资源
    最近更新 更多