C#MVC中@HTML中的方法

//生成表单

@{ 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 = "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 = "200px; height:100px;" })

@Html.TextBoxFor(model => model.AccountName, new { @style = "200px;" })

@Html.EditorFor(model => model.UserPassword)

@Html.PasswordFor(m => m.PromoterPwd, new { @class = "input_txt", dataType = "LimitB", min = "6", max = "20", msg = "密码为6到20个字符" })

原文地址:https://www.cnblogs.com/wfy680/p/12213292.html