日期:2014-05-17  浏览次数:20537 次

关于mvc3的远程验证,找了很久的原因还是没找出来
前台  
@Html.ValidationSummary(true)
<div class="editor-label">
  @Html.LabelFor(model => model.CoustomeAdID)
  </div>
  <div class="editor-field">
  @Html.EditorFor(model => model.CoustomeAdID)
  @Html.ValidationMessageFor(model => model.CoustomeAdID)
  </div>
模型
[System.ComponentModel.DataAnnotations.Key]
  [RegularExpression(@"\d{17}[\d|X]|\d{15}", ErrorMessage = "{0}的格式不正确")]
  [Display(Name = "身份证号码")]
  [Remote("CheckIDExists", "Coustoms", ErrorMessage = "该身份证信息已经存在")]
  public string CoustomeAdID { get; set; }

  验证模式 [HttpGet] // 只能用GET !!!
  public ActionResult CheckIDExists(string UserID)
  {
  var coustomes = db.Database.SqlQuery<CoustomsDetail>("Select * from CoustomsDetail where CoustomeAdID = {0}",UserID);
  return Json(coustomes.ToList().Count==0, JsonRequestBehavior.AllowGet);
  }
获取的UserID一直为空

------解决方案--------------------
貌似应该这样.
public ActionResult CheckIDExists(CoustomsDetail coustomsDetail)
{
var coustomes = db.Database.SqlQuery<CoustomsDetail>("Select * from CoustomsDetail where CoustomeAdID = {0}",coustomsDetailUserID);
return Json(coustomes.ToList().Count==0, JsonRequestBehavior.AllowGet);
}