4
我正在使用数据注释验证程序并且它不工作的ASP .net MVC项目。我是新来的MVC。请帮我在这注解验证在ASP中不起作用.net MVC
我的模型
public class Home
{
public int i;
[Required(ErrorMessage="Please enter")]
[StringLength(160)]
public string name;
}
我的控制器
public ActionResult Index()
{
Home h = new Home();
return View(h);
}
[HttpPost]
public ActionResult Index(Home h)
{
if (ModelState.IsValid)
{
return RedirectToAction("Success");
}
//ModelState.AddModelError("name", "Enter name");
return View(h);
}
我查看
@using (Html.BeginForm())
{
<label for="name">Name: </label>
@Html.TextBoxFor(m=>m.name)
@Html.ValidationMessageFor(m=>m.name)
<input type="submit" value="Register" />
}
非常感谢ramiramilu。它正在工作。 – Vishnu