1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/// <summary>
/// 用户添加操作的模型
/// </summary>
public
class
MyUser_AddModel
{
#region MyRegion
/// <summary>
/// 用户名
/// </summary>
[DisplayName(
"登录账号"
)]
[Required(ErrorMessage =
"用户账号不能为空"
)]
[Remote(
"CheckUserAccountExists"
,
"Test"
, ErrorMessage =
"用户账号已存在"
)]
// 远程验证(Ajax)
public
string
UserAccount {
get
;
set
; }
}
|
1
2
3
4
5
6
7
|
[HttpGet]
// 只能用GET !!!
public
ActionResult CheckUserAccountExists(
string
UserAccount)
{
string
[] existsUsers = {
"youguanbumen"
,
"wodanwojun"
};
bool
exists =
string
.IsNullOrEmpty(existsUsers.FirstOrDefault(u => u.ToLower() == UserAccount.ToLower())) ==
false
;
return
Json(!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
>
|
1
2
3
4
5
6
7
8
9
10
11
|
[HttpPost]
public
ActionResult CheckName(
string
Name)
{
if
(Name ==
"bjs007"
)
{
return
Json(
"用户名重复"
, JsonRequestBehavior.AllowGet);
}
return
Json(
true
, JsonRequestBehavior.AllowGet);
}
[Remote(
"CheckName"
,
"Home"
,HttpMethod=
"POST"
)]
public
string
Name {
get
;
set
; }
|