Asp.net mvc Dropdownlist客户端需要验证(无模型)

Asp.net mvc Dropdownlist客户端需要验证(无模型),asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,查看: @Html.DropDownList("CategoryItems", null, new { @class = "ddlcs" }) @Html.ValidationMessage("CategoryItems") var cat = from s in db.CategoryDbSet where s.IsActive == true orderby s.CatName select new { s.CatID, s.Ca

查看

@Html.DropDownList("CategoryItems", null, new { @class = "ddlcs" })
@Html.ValidationMessage("CategoryItems")
var cat = from s in db.CategoryDbSet
          where s.IsActive == true
          orderby s.CatName
          select new { s.CatID, s.CatName };

var catListItems = cat.ToList()
                      .Select(c => new SelectListItem   
                             {
                                 Text = c.CatName,
                                 Value = c.CatID.ToString()
                             })
                       .ToList();

catListItems.Insert(0, new SelectListItem 
                           {
                               Text = "[--Select the category--]", 
                               Value = "" 
                           });

ViewBag.CategoryItems = catListItems;
控制器

@Html.DropDownList("CategoryItems", null, new { @class = "ddlcs" })
@Html.ValidationMessage("CategoryItems")
var cat = from s in db.CategoryDbSet
          where s.IsActive == true
          orderby s.CatName
          select new { s.CatID, s.CatName };

var catListItems = cat.ToList()
                      .Select(c => new SelectListItem   
                             {
                                 Text = c.CatName,
                                 Value = c.CatID.ToString()
                             })
                       .ToList();

catListItems.Insert(0, new SelectListItem 
                           {
                               Text = "[--Select the category--]", 
                               Value = "" 
                           });

ViewBag.CategoryItems = catListItems;
当有人在保存操作期间选择“选择类别”选项时,我希望在下拉列表上强制执行所需的验证。我是MVC框架的新手,我不知道我在哪里犯了错误?此下拉列表与模型无关

请推荐soln

此下拉列表与模型无关

这是个错误。ASP.NET MVC中的验证通过使用相应的属性装饰视图模型属性来工作。例如,如果希望使此下拉列表成为必需的,则可以使用
[required]
属性装饰视图模型上的相应属性

因此,请向现有视图模型添加必要的属性:

public class MyViewModel
{
    [Required]
    public int? SelectedCategoryId { get; set; }

    public IEnumerable<SelectListItem> Categories { get; set; }

    ... some other properties that your view might need
}
var model = new MyViewModel();
model.Categories = cat
    .ToList()
    .Select(c => new SelectListItem   
    {
        Text = c.CatName,
        Value = c.CatID.ToString()
    }).ToList();

return View(model);
在您看来,请使用强类型版本的帮助程序:

@Html.DropDownListFor(
    x => x.SelectedCategoryId,
    Model.Categories, 
    "[--Select the category--]", 
    new { @class = "ddlcs" }
)
@Html.ValidationMessageFor(x => x.SelectedCategoryId)

如果只需要客户端验证,可以执行以下操作:

$('form').validate({
    rules:{
        CategoryItems: 'required'
    }
});


但我不建议这样做,因为客户端验证是为了获得更好的用户体验,而且可以轻松绕过。Darin的回答中描述了正确的方法,使用了数据注释和视图模型。

感谢您的帮助。但是我使用EF将db对象映射到模型中。我开始在使用此模型的其他地方收到错误消息。System.Data.SqlClient.SqlException:列名“SelectedCategoryId”无效。现在我如何修复其他位置的此新属性。您不应该在视图中使用EF模型。这就是视图模型的用途。您的控制器可以在2之间进行映射。在列表页面上,我正在执行类似于public ActionResult Index(){ViewBag.Message=“广告列表”;return View(db.advision.ToList());}的操作。您能建议我如何在控制器中进行映射吗?