Reputation: 299
I have simple MVC code having a form with validation.but my validation for name property(required) does not work.I create BreakPoint in my controller after "InsertStudent" action result.
so I run that and fill the form without filling the name text box and I expect "IsValid" in my controller to be false.but it is true!!!
my view is:
@model HelloWorld.Models.student
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>AddStudent2</title>
</head>
<body>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm("InsertStudent","home",FormMethod.Post)) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>student</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Family)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Family)
@Html.ValidationMessageFor(model => model.Family)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</body>
</html>
and this is my controller:
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult index()
{
return View("home");
}
public ActionResult addstudent()
{
student stud = new student();
return View(stud);
}
public ActionResult InsertStudent(student stud)
{
if (ModelState.IsValid==true)
{
//sahih
}
else{
//ghalat
}
return View("addstudent");
}
public ActionResult AddStudent2()
{
student stud2 = new student();
return View(stud2);
}
}
}
and model is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace HelloWorld.Models
{
public class student
{
public int Id { get; set; }
public string Name { get; set; }
[Required]
public string Family { get; set; }
[Required]
//[MaxLength(5)]
public int Age { get; set; }
public string Email { get; set; }
}
}
please help me
Upvotes: 0
Views: 449
Reputation: 299
I was able to solve the problem.
I had to put the [required]
attribute below my name
property instead of putting it above.
public class student
{
public int Id { get; set; }
[Required(AllowEmptyStrings = false)]
public string Name { get; set; }
[Required(AllowEmptyStrings=false)]
public string Family { get; set; }
[Required(AllowEmptyStrings = false)]
//[MaxLength(5)]
public int Age { get; set; }
public string Email { get; set; }
}
Upvotes: 1
Reputation: 4771
You do not have a Required
attribute on your Name property. Add it and it the validation should fail
public class student
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Family { get; set; }
[Required]
public int Age { get; set; }
public string Email { get; set; }
}
Upvotes: 1