//生成表單
@{ Html.BeginForm("Index", "Simple", FormMethod.Post, new { id = "myForm" }); }
@*表單內容*@
@{ Html.EndForm();}
或者
@using (Html.BeginForm("AccountTypeForm", "Account", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@*表單內容*@
}
//生成復選框
@Html.CheckBox("checkBox",new { id="myCheckBox" })
//生成下拉列表框
@{ var dropList = new List<SelectListItem>();
for (int i = 0; i < 5; i++)
{
var dropItem = new SelectListItem();
dropItem.Value = i.ToString();
dropItem.Text = i.ToString();
dropList.Add(dropItem);
}
}
@Html.DropDownList("myList", dropList, new { style = "width:100px;" })
//生成超鏈接
@Html.ActionLink(" >> 返回列表", "AccountTypeList")
//生成隱藏文本
@Html.HiddenFor(model => model.ID)
//顯示錯誤的控件
@Html.ValidationSummary(true)
@*當后台if (ModelState.IsValid)失敗后,錯誤信息就會顯示到 @Html.ValidationSummary()*@
@*當前后台驗證都通過,但某些邏輯驗證沒有通過,比如用記名密碼錯誤的,可以手工添加錯誤信息,
ModelState.AddModelError("", "The current password is incorrect or the new password is invalid."); 這個也會顯示到@Html.ValidationSummary()*@
//根據后台定義添加前台js驗證的控件
@Html.ValidationMessageFor(model => model.UserPassword)
@*
[Display(Name="password")]
[DataType(DataType.Password)]
[Required(AllowEmptyStrings = false, ErrorMessage = "密碼不能為空")]
[StringLength(60, MinimumLength = 20, ErrorMessage = "密碼必須在{2} 和{1}之間")]
public string UserPassword { get; set; }
類似的有
[StringLength(20, MinimumLength = 6, ErrorMessage = "用戶名不能大於{2} 且要小於{1}")]
[Compare("Email", ErrorMessage = "郵箱要相同")]
[RegularExpression(@"\d{17}[\d|x]|\d{15}", ErrorMessage = "身份證號碼格式錯誤")]
[Range(10, 100, ErrorMessage = "年齡不能大於{2} 不能小於{1}")]
[Required(ErrorMessage = "金額不能為空")]
[Range(typeof(decimal), "20.0", "30.0", ErrorMessage = "金額在{1}和{2}之間")]
*@
//文本
@Html.LabelFor(model => model.AccountDescription)
//常用控件
@Html.TextAreaFor(model => model.AccountDescription, new { @style = "width:200px; height:100px;" })
@Html.TextBoxFor(model => model.AccountName, new { @style = "width:200px;" })
@Html.EditorFor(model => model.UserPassword)
@Html.PasswordFor(m => m.PromoterPwd, new { @class = "input_txt", dataType = "LimitB", min = "6", max = "20", msg = "密碼為6到20個字符" })