【发布时间】:2016-03-09 10:57:53
【问题描述】:
对我目前使用的所有其他元素进行验证,但没有显示编辑器的验证消息。 ModelState 具有正确的错误状态, 但在表单上看不到任何消息。
这是我所拥有的:
型号:
[Required(AllowEmptyStrings = false, ErrorMessage = "Diese Feld muss angegeben werden.")]
[StringLength(8000,ErrorMessage = "Feld muss zwischen {2} und {1} Zeichen enthalten.", MinimumLength = 8)]
[DataType(DataType.MultilineText)]
[AllowHtml]
[Display(Name = "Beschreibung")]
public string Description { get; set; }
查看:
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
这也不起作用:
@Html.ValidationMessageFor(model => model.Description)
当我这样做时:
@Html.ValidationMessageFor(model => model.Description, "test message")
它有效 - 意思是,它显示“测试消息” - 但我想要我在模型中定义的消息......
----这是一些按预期工作的字段----
型号:
[Required(ErrorMessage = "Dieses Feld muss angegeben werden.")]
[StringLength(25, MinimumLength = 2)]
[Display(Name = "KeyWord")]
public string Keyword { get; set; }
查看:
<div class="form-group">
@Html.LabelFor(model => model.Keyword, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Keyword, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Keyword, "", new { @class = "text-danger" })
</div>
</div>
当字段为空并尝试提交时,将显示所需的消息。 当字段包含的字符太少或太多,并且字段不在焦点或尝试提交时,显示字符串长度消息.. 这就是我想要的第二编辑...
-----工作区结束-----
我做错了什么?
谢谢
已解决: 就是用“jQuery TE”来美化编辑器。
感谢@Stephen Muecke 和@Varun Vasishtha 把我推到那里;)
【问题讨论】:
-
您是说如果您将 textarea 留空,就不会收到错误消息?
-
基本上是的,但是当我输入 1 个字符时它也没有显示(只是为了让它不为空)
-
您显示的代码工作正常。我认为还有一些其他代码导致了问题
-
请参考this DotNetFiddle 以证明它与您显示的代码一起工作。您是否有机会为隐藏元素的文本区域使用 jquery 插件(因此不会在客户端上进行验证)?
标签: asp.net asp.net-mvc razor