开发者

Remote validator with DropDownList in ASP.NET MVC 3

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 t开发者_如何学Pythonext input are working perfectly. Am I doing something wrong or is there a problem with DropDownList Remote validation?

Thanks!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜