我在使用DropDownList验证的ASP.NET MVC中遇到问题。 我有两个动作“创建”。它们定义如下:
public ActionResult Create()
{
var categoriasDownloads = from catDown in modelo.tbCategoriasDownloads
orderby catDown.TituloCategoriaDownload ascending
select catDown;
ViewData["CategoriasDownloads"] = new SelectList(categoriasDownloads, "IDCategoriaDownloads", "TituloCategoriaDownload");
var formatosArquivos = from formatosDown in modelo.tbFormatosArquivos
orderby formatosDown.NomeFormatoSigla
select formatosDown;
ViewData["FormatosArquivos"] = new SelectList(formatosArquivos, "IDFormatoArquivo", "NomeFormatoSigla");
return View();
}
,第二个动作是:
[HttpPost]
public ActionResult Create(tbDownloads _novoDownload)
{
TryUpdateModel(modelo);
TryUpdateModel(modelo.tbDownloads);
if (ModelState.IsValid)
{
modelo.AddTotbDownloads(_novoDownload);
modelo.SaveChanges();
return RedirectToAction("Sucesso", "Mensagens");
}
return View(_novoDownload);
}
问题是:尝试验证时,验证不会发生。我正在使用数据注释来验证,但我还没有成功。
我该怎么办?
由于
答案 0 :(得分:0)
验证发生但您正在验证错误的对象。
WRONG:
TryUpdateModel(modelo);
TryUpdateModel(modelo.tbDownloads);
正确:
TryUpdateModel(_novoDownload);
答案 1 :(得分:0)
$。validator.addMethod( “选择无”,
function (value, element) {
return this.optional(element) || element.selectedIndex != 0;
},
"Please select an option."
);
$(function () {
$("#form1").validate({
rules: {
ProductCategoryList: {
selectNone: true
}
},
messages: {
ProductCategoryList: {
selectNone: "This field is required"
}
}
});
});