ASP.NET MVC 3 中带有 DropDownList 的远程验证器
我正在尝试(没有成功)在 DropDownList 上使用远程验证器:
// Person.cs
public int PersonID { get; set; }
public string Name { get; set; }
// Card.cs
public int CardID { get; set; }
[Remote("PersonValidation", "Validation", ErrorMessage = "...")]
public int PersonID { get; set; }
public virtual Person Person { get; set; }
// CardController
public ActionResult Create()
{
ViewBag.PersonID = new SelectList(db.Persons, "PersonID", "Name");
Card card = new Card();
return View(card);
}
// create.cshtml (Card Views)
<div class="editor-label">@Html.LabelFor(model => model.personID, "Person")</div>
<div class="editor-field">
@Html.DropDownList("PersonID", String.Empty)
@Html.ValidationMessageFor(model => model.PersonID)
</div>
// ValidationController.cs
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
public JsonResult PersonValidation(int id)
{
Person person = db.Persons.Find(id);
return Json(person.Cards.Count > 0, JsonRequestBehavior.AllowGet);
}
PersonValidation 永远不会被触发。其他带有文本输入的“远程”验证运行良好。 我做错了什么或者 DropDownList 远程验证有问题吗?
谢谢!
I'm trying (without success) to use the Remote validator on a DropDownList:
// Person.cs
public int PersonID { get; set; }
public string Name { get; set; }
// Card.cs
public int CardID { get; set; }
[Remote("PersonValidation", "Validation", ErrorMessage = "...")]
public int PersonID { get; set; }
public virtual Person Person { get; set; }
// CardController
public ActionResult Create()
{
ViewBag.PersonID = new SelectList(db.Persons, "PersonID", "Name");
Card card = new Card();
return View(card);
}
// create.cshtml (Card Views)
<div class="editor-label">@Html.LabelFor(model => model.personID, "Person")</div>
<div class="editor-field">
@Html.DropDownList("PersonID", String.Empty)
@Html.ValidationMessageFor(model => model.PersonID)
</div>
// ValidationController.cs
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
public JsonResult PersonValidation(int id)
{
Person person = db.Persons.Find(id);
return Json(person.Cards.Count > 0, JsonRequestBehavior.AllowGet);
}
The PersonValidation is never fired. The others "Remote" validations with text input are working perfectly.
Am I doing something wrong or is there a problem with DropDownList Remote validation?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
验证器不会触发,因为您需要使用 @Html.DropDownListFor() 来创建带有“data-val”元素的 HTML 元素,该元素将被解析为不显眼的验证器。
The validator does not fire because you need to use
@Html.DropDownListFor()
in order to create an HTML element with "data-val" elements, which will be parsed into unobtrusive validators.