ASP.NET MVC 3 中带有 DropDownList 的远程验证器

发布于 2024-11-29 08:22:38 字数 1147 浏览 0 评论 0原文

我正在尝试(没有成功)在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看轻我的陪伴 2024-12-06 08:22:39

验证器不会触发,因为您需要使用 @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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文