今天为大伙介绍:ASP.NET MVC3 RemoteAttribute远程属性验证的相关内容。
对于:MVC3 RemoteAttribute,是项新内容,话称为:MVC Model远程验证。
下面看下相关介绍:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
/// <summary>/// 用户添加操作的模型/// </summary>publicclassMyUser_AddModel{ #region MyRegion /// <summary> /// 用户名 /// </summary> [DisplayName("登录账号")] [Required(ErrorMessage = "用户账号不能为空")] [Remote("CheckUserAccountExists", "Test", ErrorMessage = "用户账号已存在")] // 远程验证(Ajax) publicstringUserAccount { get; set; }} |
|
1
2
3
4
5
6
7 |
[HttpGet] // 只能用GET !!!publicActionResult CheckUserAccountExists(stringUserAccount){ string[] existsUsers = { "youguanbumen", "wodanwojun"}; boolexists = string.IsNullOrEmpty(existsUsers.FirstOrDefault(u => u.ToLower() == UserAccount.ToLower())) == false; returnJson(!exists, JsonRequestBehavior.AllowGet);} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 |
@model MvcApplication.Models.MyUser_AddModel <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script><fieldset> <legend>添加用户</legend> <div> @using(Html.BeginForm("DoAddUser","Test")){ <p> @Html.LabelFor(m=>m.UserAccount): @Html.TextBoxFor(m=>m.UserAccount) @Html.ValidationMessageFor(m=>m.UserAccount) </p> <p> <input type="submit" value="提 交" /> </p> } </div></fieldset> |

内容ASP.NET MVC3 RemoteAttribute远程属性验证教程就介绍到这里了。
2011/9/9 0:07:19 | ASP.NET MVC教程 | |