ASP.NET 带有部分 class 的 MVC 数据注释不起作用
ASP.NET MVC data annotations with partial class doesn't work
我已经应用了一些带有数据注释的验证,但不知何故我在代码中遗漏了一些东西。
public class Person
{
public people SinglePerson { get; set; }
public IEnumerable<SelectListItem> ColorNames { get; set; }
public IEnumerable<SelectListItem> WebCustomer { get; set; }
public IEnumerable<SelectListItem> PreviouslyOredered { get; set; }
}
这是我的 cs class
[MetadataType(typeof(peopleMetaData))]
public partial class people
{
}
public class peopleMetaData
{
[Required(ErrorMessage = "Please enter a name")]
[StringLength(50, MinimumLength = 2)]
public string firstName { get; set; }
}
People
class 有一个 firstName
属性,我想对其进行一些验证。
我错过了什么?
你必须检查控制器中的模型是否有效
if (ModelState.IsValid) {
//do something
}
else
{
return View();
}
你们 Class 没有 属性 firstName
。
您首先需要在您的人员 class 中使用它,然后是 MetadataTypeClass
[MetadataType(typeof(peopleMetaData))]
public partial class people
{
public string firstName { get; set; }
}
对于客户端验证,请确保您已包含
@section Scripts { @Scripts.Render("~/bundles/jqueryval") }
在你看来
在元数据 class 中,您不能将 "properties" 指定为完整属性 - 只需要类型和名称 - 试试这个:
public class peopleMetaData
{
[Required(ErrorMessage = "Please enter a name")]
[StringLength(50, MinimumLength = 2)]
public string firstName;
}
请参阅此处 firstName
"property" 的 { get; set; }
部分类只能存在于一个项目中。
http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.100).aspx
更具体地说,
All partial-type definitions meant to be parts of the same type must
be defined in the same assembly and the same module (.exe or .dll
file). Partial definitions cannot span multiple modules.
我已经应用了一些带有数据注释的验证,但不知何故我在代码中遗漏了一些东西。
public class Person
{
public people SinglePerson { get; set; }
public IEnumerable<SelectListItem> ColorNames { get; set; }
public IEnumerable<SelectListItem> WebCustomer { get; set; }
public IEnumerable<SelectListItem> PreviouslyOredered { get; set; }
}
这是我的 cs class
[MetadataType(typeof(peopleMetaData))]
public partial class people
{
}
public class peopleMetaData
{
[Required(ErrorMessage = "Please enter a name")]
[StringLength(50, MinimumLength = 2)]
public string firstName { get; set; }
}
People
class 有一个 firstName
属性,我想对其进行一些验证。
我错过了什么?
你必须检查控制器中的模型是否有效
if (ModelState.IsValid) {
//do something
}
else
{
return View();
}
你们 Class 没有 属性 firstName
。
您首先需要在您的人员 class 中使用它,然后是 MetadataTypeClass
[MetadataType(typeof(peopleMetaData))]
public partial class people
{
public string firstName { get; set; }
}
对于客户端验证,请确保您已包含
@section Scripts { @Scripts.Render("~/bundles/jqueryval") }
在你看来
在元数据 class 中,您不能将 "properties" 指定为完整属性 - 只需要类型和名称 - 试试这个:
public class peopleMetaData
{
[Required(ErrorMessage = "Please enter a name")]
[StringLength(50, MinimumLength = 2)]
public string firstName;
}
请参阅此处 firstName
"property" 的 { get; set; }
部分类只能存在于一个项目中。
http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.100).aspx
更具体地说,
All partial-type definitions meant to be parts of the same type must be defined in the same assembly and the same module (.exe or .dll file). Partial definitions cannot span multiple modules.