Asp.net mvc &引用;“必需”;“当”时属性不起作用;AllowHtml“;属性被使用。MVC5

Asp.net mvc &引用;“必需”;“当”时属性不起作用;AllowHtml“;属性被使用。MVC5,asp.net-mvc,validation,asp.net-mvc-5,Asp.net Mvc,Validation,Asp.net Mvc 5,我想发布一个带有所见即所得编辑器的html,所以我在我的属性上使用了[allowtml]属性。它可以工作,但是当我将它与[Required]和[StringLength]属性一起使用时,它会停止工作ModelState.IsValid即使prop为空也返回true 是的,我知道如果它为空,我可以手动控制它,并将错误添加到ModelState,但为什么 为什么会发生这种情况?有没有更好的方法将一些HTML代码发布到后端 我的dto: public int Id { get; set; } [Re

我想发布一个带有所见即所得编辑器的html,所以我在我的属性上使用了
[allowtml]
属性。它可以工作,但是当我将它与
[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")" />
}
@使用(Html.BeginForm())
{
@Html.HiddenFor(m=>m.Id)
名字*
@TextBoxFor(m=>m.Name,新的{@class=“form control”})
@ValidationMessageFor(m=>m.Name,null,新的{@class=“label-label-danger”})
内容*
@Html.TextBoxFor(m=>m.HtmlBody)
@ValidationMessageFor(m=>m.HtmlBody,null,新的{@class=“label-label-danger”})
}

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

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

接下来,请注意,我在
[必需(
属性)中添加了
AllowEmptyStrings=false
。这可能是您缺少的重要部分(将不进行测试)测试似乎表明未设置AllowEmptyStrings不会影响此测试设置的结果。模型仍然无效。

设置

  • 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>
    }
    
    @model WebApplication6.Models.TestViewModel
    @{
    ViewBag.Title=“关于”;
    }
    HTML的测试
    @使用(Html.BeginForm())
    {
    内容*
    @Html.TextAreaFor(m=>m.HtmlBody)
    @ValidationMessageFor(m=>m.HtmlBody,null,新的{@class=“label-label-danger”})
    }
    @节脚本
    {
    $(函数(){
    $('#HtmlBody').ckeditor();
    });
    }
    
    结果: 基本上,引发异常是因为模型状态无效

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

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

    接下来,请注意,我在
    [必需(
    属性)中添加了
    AllowEmptyStrings=false
    。这可能是您缺少的重要部分(将不进行测试)测试似乎表明未设置AllowEmptyStrings不会影响此测试设置的结果。模型仍然无效。

    设置

  • 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>
    }
    
    @model WebApplication6.Models.TestViewModel
    @{
    ViewBag.Title=“关于”;
    }
    HTML的测试
    @使用(Html.BeginForm())
    {
    内容*
    @Html.TextAreaFor(m=>m.HtmlBody)
    @ValidationMessageFor(m=>m.HtmlBody,null,新的{@class=“label-label-danger”})
    }
    @节脚本
    {
    $(函数(){
    $('#HtmlBody').ckeditor();
    });
    }
    
    结果: 基本上,引发异常是因为模型状态无效 我发现了这个问题

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

    我发现了问题


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

    您是否使用了tiny mce或Ckeditor之类的富文本编辑器?@MDDDC是的,我使用的是Ckeditor,但我尝试了常规文本框和区域,但都是一样的……是吗获取关于HTmlBody的post方法的任何数据?你能放一个刹车点看看吗?@MDDDC它似乎为空,我的朋友。@gkon,“它似乎为空,我的朋友。”你这是什么意思?它要么是空的,要么不是空的。你设置了断点吗?你使用的是像tiny mce或Ckeditor这样的富文本编辑器吗?@MDDDC是的,我使用Ckeditor,但我用一个常规的文本框和区域尝试过,但它是一样的……你得到了HTmlBody的post方法的任何数据吗?你能放一个刹车点看看吗?@MDDDC它似乎是空的我的朋友。@gkon,“我的朋友,这似乎是无效的。”你这是什么意思?它要么为空,要么不为空。你设置断点了吗?谢谢Tommy。我会尝试AllowEmptyStrings。而且,我知道textbox不适用于ckeditor,但在此之前我一直在使用它,我将它改为textbox只是为了测试。@gkon-我的测试表明它没有什么区别,它应该是默认的false。'AllowEmptyStrings'不起作用oo。我也创建了另一个项目,但也像你一样出现了错误。我尝试了不使用属性路由、验证属性的每一个组合和许多不同的post数据,但都是一样的。“AllowHtml”阻止所有其他属性工作。@gkon-它只阻止它在你的一个项目中工作。该控件/p还存在其他问题属性。例如,确保ckEditor没有发送空标记。这里的任何人都可以帮助您,直到我们可以推断出与您的配置和开箱即用实现的不同之处。谢谢Tommy。我将尝试AllowEmptyString。而且,我知道textbox