asp.net mvc-c#mvc验证文本
本文关键字:文本 验证 mvc-c#mvc net asp | 更新日期: 2023-09-27 18:00:22
我有性别编辑栏,我想使其有效(男性或女性),有我的模型类:
public int CustomerID { get; set; }
public int BranchID { get; set; }
public string FirstName { get; set; }
public string SirName { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
这是我想要有效的观点部分:
<div class="editor-label">
@Html.LabelFor(model => model.Gender)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Gender)
@Html.ValidationMessageFor(model => model.Gender)
</div>
添加枚举
public enum Gender
{
Male,
Female,
}
并在模型中将属性更改为
public Gender Gender { get; set; }
并且在视图中使用属性的单选按钮
@Html.RadioButtonFor(m => m.Gender, "Male", new { id = "Gender_Male" })
<label for="Gender_Male">Male</label>
@Html.RadioButtonFor(m => m.Gender, "Female", new { id = "Gender_Female" })
<label for="Gender_Female">Female</label>
@Html.ValidationMessageFor(model => model.Gender)