Mvc3.0远程验证用户唯一性
作为一名新手,对新的东西老是学了就忘,为了快速掌握新内容,从今天开始要写笔记啦!
今天发表第一篇 ASP.NET MVC 3 中远程验证(Remote Validation)的文章。Remote Validation 其实在 MVC 3 正式版发布前就已经有了,主要作用是实现客户端的异步验证,如用户注册时检测用户名是否已被注册等。这样就如我们以前常用 Ajax 的方式检测一样,如果用户已被注册能很快的返回给用户相关信息,提高了用户体验。
看下面两幅图:第一张为添加商店时对添加管理员进行唯一性验证,第二张为管理员对商店里的员工添加和编辑信息时进行唯一性验证。这两张图添加的user最终都添加到User表中,但是添加商店管理员时使用的ShopAndUser类,管理员添加员工使用的是User类,这样一来使用系统验证会存在冲突,导致下面两处只有一处能够通过验证,因为它们的Name不同,添加商店管理员时的Name为User.LoginName,管理员添加成员时的Name为LoginName,为了解决此问题我们可以通过两种办法解决。


Model验证代码如下:

解决方法一:
可以将两个Model类设置为同一个Model,及ShopAndUser,这样在Controller的Action中可以绑定指定的Name来达到远程验证的目的。

其中User.LoginName为绑定的登录名,User.EditName为防止编辑时验证用户唯一性做的一个隐藏字段。View代码如下:
<tr>
<th>
<span class="Red">*</span> @Html.LabelFor(model => model.LoginName)
</th>
<td>
@Html.EditorFor(model => model.LoginName)
@Html.ValidationMessageFor(model => model.LoginName)
@if (Model != null) {
@Html.Hidden("EditName", Model.LoginName)
}
</td>
<th class="Th_width">
<span class="Red">*</span> @Html.LabelFor(model => model.Password)
</th>
<td>
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</td>
</tr>

远程验证唯一性成功。
解决方法二:
使用两个Model及ShopAndUser和User两个Model,如何使用对两个不同的Name进行验证呢?那当然是要建两个对应的验证Model了, 一个为UserPart,对应ShopAndUser,另一个为User,对应User。
Model中User验证代码如下:

对应的Action为:

Model中UserPart验证代码如下:

对应的Action为:

添加商店管理员时,对应的View页面如下:
@model MedShop.Remote.ViewModel.ShopAndUser @{ ViewBag.Title = "ShopCreate"; Layout = "~/Views/Shared/Blue_layer.cshtml"; }
<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>
@using (Html.BeginForm("ShopCreate", "Shop", null, FormMethod.Post, new { target = "_parent" })) {
@Html.ValidationSummary(true)
@Html.EditorFor(model => model.Shop, "Shop")
<table>
<tr>
<th width="100">
<span class="Red">*</span> 管理员姓名:
</th>
<td> @Html.EditorFor(model => model.UserPart.Name)
@Html.ValidationMessageFor(model => model.UserPart.Name)
</td>
</tr>
<tr>
<th> <span class="Red">*</span> 管理员登陆名: </th>
<td>
@Html.EditorFor(model => model.UserPart.LoginName)
@Html.ValidationMessageFor(model => model.UserPart.LoginName)
</td>
</tr>
<tr>
<th> <span class="Red">*</span> 登陆密码:
</th>
<td>
@Html.EditorFor(model => model.UserPart.Password)
@Html.ValidationMessageFor(model => model.UserPart.Password)
</td>
</tr>
</table>
<div class="LayerButton"> <input type="submit" value="确定" /></div>
}
在此提交表单时,我们必须报自定义的模型UserPart复制给系统自动模型User,及对应的User表,这样,我们才能将user 成功插入User表,其提交表单的Acton代码如下:
public ActionResult ShopCreate()
{
var user=(User)Session["user"];
ShopAndUser SAP = new ShopAndUser()
{
Shop = new Shop(),
UserPart=new UserPart()
};
ViewBag._Province = user.Shop.Province.ProvinceName;
ViewBag._City = user.Shop.City.CityName;
ViewBag._District = user.Shop.District.DistrictName;
ViewBag._Town = provinceRep.TownIndex(user.Shop.DistrictID).ToList();
return View(SAP);
}
[HttpPost]
public ActionResult ShopCreate(UserPart userPart, Shop shop, FormCollection fc) 】
{
ShopConfig shopConfig = new ShopConfig();
var user1 = (User)Session["user"];
try
{
if (ModelState.IsValid)
{
User user = new User();
user.Name = userPart.Name;
user.LoginName = userPart.LoginName;
user.Password = userPart.Password;
shop.ID = Guid.NewGuid();
shop.CreatedTime = DateTime.Now;
shop.ProvID = user1.Shop.ProvID;
shop.CityID = user1.Shop.CityID;
shop.DistrictID = user1.Shop.DistrictID;
user.ID = Guid.NewGuid();
user.ShopID = shop.ID;
user.StatusID = 0;
user.CreatedDate = DateTime.Now;
shopConfig.ShopID = shop.ID;
shopConfig.PurchaseDate = DateTime.Now;
shopRep.Add(shop, user, shopConfig, user.ID);
shopRep.Save();
return RedirectToAction("ShopIndex", new { ShopID = shop.ID });
}
}
catch {}
}

浙公网安备 33010602011771号