DropDownList 验证 - ModelState.IsValid 始终为 false
我尝试创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有正确使用
DropDownListFor
帮助程序。第一个参数表示将保存所选值的属性,并且必须是标量类型。在您的情况下,您传递的是一个集合,与用作第二个参数的集合相同。所以它应该是这样的:另外,您所说的 AnimalViewModel 也不是一个好的命名约定,因为这不是视图模型。创建一个类并将所有模型作为公共属性填充到其中是视图模型的错误想法。以下是您的视图模型的实际外观:
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: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: