DropDownList 验证 - ModelState.IsValid 始终为 false

发布于 2024-10-16 13:59:03 字数 2269 浏览 1 评论 0原文

我尝试创建一个 asp.net mvc 2 应用程序。 我的 DropDownList 将不会被验证!

我有一个名为 Animal 的核心模型类,它具有一些属性,并且 Genus 类也具有相同的属性。这些类映射到 nHibernate。


namespace Core.Models
{
    public class Animal
    {
        public Animal() { }

        public virtual int AnimalId { get; set; }

        [DisplayName("Name")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Name is required")]
        public virtual string Name { get; set; }

        [DisplayName("Gattung")]
        [Required(ErrorMessage = "Genus is required")]
        public virtual Genus Genus { get; set; }
    }
}
namespace Core.Models
{
    public class Genus
    {
        public Genus() { }

        public virtual int GenusId { get; set; }

        [DisplayName("Name")]
        public virtual string GenusTitle { get; set; }
    }
}

在我的 UI 项目中,我有一个名为 AnimalViewModel 的 ViewModel 类,


   public class AnimalViewModel
   {
        public Animal Animal { get; set; }
        public string ReturnUrl { get; set; }

        public IList Genus { get; set; }

        public AnimalViewModel(Animal a, string returnUrl)
        {
            this.Animal = a;
            this.ReturnUrl = returnUrl;            
        }

        public AnimalViewModel() { }
    }

这是我的观点:


 model.Animal.Genus, new SelectList(Model.Genus, "GenusId", "GenusTitle"), "-- Fill out --")%>

最后我的控制器:


        public ActionResult Index(string returnUrl)
        {
            AnimalViewModel avm = new AnimalViewModel()
            {
                Animal = new Animal(),
                ReturnUrl = returnUrl,
                Genus = GenusRepository().GetAll()
            };
            return View(avm);
        }
        [HttpPost]
        public ActionResult Index(AnimalViewModel avm)
        {
            if (ModelState.IsValid) //is always false
            {
                //save
                return RedirectToAction("Overview");
            }
            else
            {
                Genus = GenusRepository().GetAll();
                return View(avm);
            }
        }

ModelState.IsValid 始终为 false。我不知道出了什么问题。 客户端验证有效,但服务器验证无效。

任何帮助将不胜感激。

谢谢!

I try to create a asp.net mvc 2 application.
My DropDownList won't be validated!

I have a core model class called Animal with some attributes and the same for the class Genus. These classes are mapped to nHibernate.


namespace Core.Models
{
    public class Animal
    {
        public Animal() { }

        public virtual int AnimalId { get; set; }

        [DisplayName("Name")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Name is required")]
        public virtual string Name { get; set; }

        [DisplayName("Gattung")]
        [Required(ErrorMessage = "Genus is required")]
        public virtual Genus Genus { get; set; }
    }
}
namespace Core.Models
{
    public class Genus
    {
        public Genus() { }

        public virtual int GenusId { get; set; }

        [DisplayName("Name")]
        public virtual string GenusTitle { get; set; }
    }
}

In my UI Project I have a ViewModel class called AnimalViewModel


   public class AnimalViewModel
   {
        public Animal Animal { get; set; }
        public string ReturnUrl { get; set; }

        public IList Genus { get; set; }

        public AnimalViewModel(Animal a, string returnUrl)
        {
            this.Animal = a;
            this.ReturnUrl = returnUrl;            
        }

        public AnimalViewModel() { }
    }

Here's my view:


 model.Animal.Genus, new SelectList(Model.Genus, "GenusId", "GenusTitle"), "-- Fill out --")%>

And finally my Controller:


        public ActionResult Index(string returnUrl)
        {
            AnimalViewModel avm = new AnimalViewModel()
            {
                Animal = new Animal(),
                ReturnUrl = returnUrl,
                Genus = GenusRepository().GetAll()
            };
            return View(avm);
        }
        [HttpPost]
        public ActionResult Index(AnimalViewModel avm)
        {
            if (ModelState.IsValid) //is always false
            {
                //save
                return RedirectToAction("Overview");
            }
            else
            {
                Genus = GenusRepository().GetAll();
                return View(avm);
            }
        }

ModelState.IsValid is always false. I have no idea what's wrong.
The client validation works but the server validation doesn't.

Any help would be appreciated.

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

述情 2024-10-23 13:59:03

您没有正确使用 DropDownListFor 帮助程序。第一个参数表示将保存所选值的属性,并且必须是标量类型。在您的情况下,您传递的是一个集合,与用作第二个参数的集合相同。所以它应该是这样的:

<%: Html.DropDownListFor(
    model => model.Animal.Genus.GenusId, 
    new SelectList(Model.Genus, "GenusId", "GenusTitle"), 
    "-- Fill out --"
)%>

另外,您所说的 AnimalViewModel 也不是一个好的命名约定,因为这不是视图模型。创建一个类并将所有模型作为公共属性填充到其中是视图模型的错误想法。以下是您的视图模型的实际外观:

public class AnimalViewModel
{
    [DisplayName("Name")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Name is required")]
    public string AnimalName { get; set; }

    [DisplayName("Gattung")]
    [Required(ErrorMessage = "Genus is required")]    
    public int? SelectedGenusId { get; set; } 

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

You are not using the DropDownListFor helper properly. The first argument represents a property that will hold the selected value and must be of a scalar type. In your case you are passing a collection, the same one used as second argument. So It should be like this:

<%: Html.DropDownListFor(
    model => model.Animal.Genus.GenusId, 
    new SelectList(Model.Genus, "GenusId", "GenusTitle"), 
    "-- Fill out --"
)%>

Also what you are calling a AnimalViewModel is not a good naming convention because this is not a view model. Creating a class and stuffing all your models inside as public properties is a false idea of a view model. Here's how your view model might really look:

public class AnimalViewModel
{
    [DisplayName("Name")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Name is required")]
    public string AnimalName { get; set; }

    [DisplayName("Gattung")]
    [Required(ErrorMessage = "Genus is required")]    
    public int? SelectedGenusId { get; set; } 

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