我有一个包含多个控件的表单。我添加了数据注释,以便在必填字段为空时向用户显示错误消息。 TextBoxes和其他控件显示消息,标签名称前面是“*”,但下拉列表不显示。一旦用户清除了所有错误消息并再次单击“提交”,则会显示下拉框的消息。如何强制下拉框与文本框同时显示消息?
其他信息: 以下是我的数据注释示例:
[MetadataType(typeof(UserMetaData))]
public partial class UserMeta
{
}
public class UserMetaData
{
[Required(ErrorMessage = "*")]
public int GenderID { get; set; }
//The gender ID is displayed in a dropdown
list with "Select" as the default option. Then, it has all the other genders
showing in the dropdown once it is clicked on.
[Required(ErrorMessage = "*")]
public DateTime DataOfBirth{ get; set; }
}
In my view, I am using
@Html.ValidationMessageFor(model => model.DataOfBirth) //This is working fine.
@Html.ValidationMessageFor(model => model.GenderID)
//Here is the dropdown
@Html.DropDownList("GenderID", null, "Select", new { style = "width:200px;", onchange = "ValidateDropdown()" })
//This is the one that is not working as expected. I could get it to work using javascript, but I am trying not to it if I there is a way to get it to work properly using data annotation.
Thank in advance for your help.
答案 0 :(得分:0)
我无法让它在服务器端正常工作。因此,我必须编写一个JQuery函数来对客户端的下拉列表进行验证。然后,只有在下拉列表中有选择时才会发布帖子。