我想知道我的班级'属性可以包括DataAnnotations作为属性,然后在ASP MVC 4中使用?此类是WCF Web服务的一部分。 UserAccount类的一个示例:
[DataMember]
[Required(ErrorMessage="Name is required")]
[StringLength(50,
ErrorMessage = "The number of characters is larger than the maximum allowed.(The maximum number of characters is 50)")]
public string Name
{
get { return name; }
set { name = value; }
}
然后在ASP.NET MVC 4视图中:
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name)<br />
@Html.ValidationMessageFor(m => m.Name)
问题是我没有得到&#34;姓名是必需的&#34;输入字段为空时作为错误消息。 这是视图的控制器:
[HttpPost]
public ActionResult AddUser(UserAccount uAccount)
{
if (!ModelState.IsValid)
{
return View();
}
string a = string.Empty;
smc.AddUserAccount(uAccount.Name,
uAccount.LastName,
uAccount.Address,
uAccount.UserName,
uAccount.UserMail,
uAccount.Password,
"admin");
return Redirect("#");
}
我在indexController中有什么遗漏吗?
答案 0 :(得分:0)
继承自ApiController
...
[ApiController]
class YourController : ControllerBase
{
....
}