J'ai un simple code MVC ayant un formulaire avec validation. Mais ma validation pour la propriété name (obligatoire) ne fonctionne pas. Je crée BreakPoint dans mon contrôleur après le résultat de l'action "InsertStudent".pourquoi la validation mvc ne fonctionne pas dans mon code?
donc je l'exécute et remplis le formulaire sans remplir la zone de texte de nom et je m'attends à ce que "IsValid" dans mon contrôleur soit faux. Mais c'est vrai !!!
mon avis est:
@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>
et ceci est mon contrôleur:
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);
}
}
}
et modèle:
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; }
}
}
s'il vous plaît me aider
Votre propriété "Nom" n'est pas requise. Votre propriété "Famille" est. C'est une faute de frappe? –
oui c'est vrai – bami
Vous devez mettre à jour votre exemple et votre code pour ajouter l'attribut requis dans la propriété Name. Faites-nous savoir si la validation n'échoue toujours pas. – Wndrr