如何为@ HTML.TextBox()显示验证消息?

时间:2014-01-22 14:09:47

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

我有以下快照代码:

我的ViewClass看起来像这样:

...
<%=Html.ValidationSummary() %>
...

<div class="editor-field-short">
   <%: Html.TextBox("Identity", Model.Identity, new Dictionary<string, object>
   {
      { "class", "textbox DependantIdPassportTextBox" },
      { "data-val", "true" },
      { "data-val-required", "Please enter the Identity Number" },                     
   },)%>
  <div class="editor-container"><%=Html.ValidationMessageFor(m => m.Identity)%></div>
</div>

控制器类

public ActionResult ValidateMember(String Identity)
{
...
var memberIdNo = MemberProfileService.GetProfileByIdNumber(Identity);
 if (memberIdNo == null)
 {
   ModelState.AddModelError("Identity", "Member does not exist");
   return View("Index");
 }
 ...

因此,如果MemberIdNo == Null为true,则错误消息“Member is not exists”不会显示在我的ViewClass中。

更新

所以我得到以下内容: 你调用的对象是空的。 在这一行:&lt;%:Html.TextBox(“Identity”,Model.Identity,new Dictionary

1 个答案:

答案 0 :(得分:0)

我明白了。

我用过:&lt;%:Html.TextBox For (...)并删除了这一行:

<div class="editor-container"><%=Html.ValidationMessageFor(m => m.Identity)%></div>

打印每条验证消息:&lt;%= Html.ValidationSummary()%&gt;

相关问题