0

我制作了一个 MVC2 项目

看法:

<%= Html.DropDownListFor ( m => m.CategoryId, Model.CategoryList )%>
 <%= Html.ValidationMessageFor ( m => m.CategoryId )%>
  <br />
  <%if ((Html.TextBoxFor(m => m.NewCategory)).Equals("")) %>
    <%{ show error msg near the textbox "* field is required"}%>
     <% else %>
   <%= Html.TextBoxFor(m => m.NewCategory)%>

如果文本框为空则显示错误消息并让用户再次输入,如何做到这一点?:S

4

1 回答 1

1

为模型的属性添加“[必需]”条件。

public class MyModel {
   [Required]
   public string NewCategory { get; set; }
}

可以在此处找到完整的解释示例:http: //www.codeproject.com/Articles/220025/A-sample-on-Asp-Net-MVC-Model-Validation-using-Dat

于 2013-11-22T09:20:28.497 回答