Folgen ist mein Code und ich weiß nicht, warum mein Validator nicht funktioniert Erster Code ist User-Modell, dann zweite ist Heimat-Controller und zuletzt ist Sicht auf das Ergebnis der Index-Aktion .Ich bin neu bei MVC.COuld Sie bitte helfen Sie mir. DankRequire Feld Validator funktioniert nicht in ASP.NET MVC
public class User
{
[Required(ErrorMessage = "UserName is required")]
public string UserName { get; set; }
[Required(ErrorMessage = "Password is required")]
public string Pasword { get; set; }
}
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string uname,string pass)
{
if(ModelState.IsValid)
{
User user = new User();
user.UserName = uname;
user.Pasword = pass;
string Username = ConfigurationManager.AppSettings["username"].ToString();
string passwrd = ConfigurationManager.AppSettings["password"].ToString();
if (user.UserName !=null && user.Pasword !=null)
{
if(user.UserName==Username && user.Pasword==passwrd)
{
return RedirectToAction("Detail", "Home");
}
}
}
return View("Index");
}
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Pasword, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Pasword, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Pasword, "", new { @class = "text-danger" })
</div>
</div>
Stellen Sie sicher, dass beide js nach jquery min.js unobtrusive.js und jquery validation.js auf der Seite oder Layout hinzugefügt haben – Curiousdev
welches Modell Sie verwenden? ist es vom Typ Benutzer? Eine weitere Sache, sollten Sie lieber Modell lieber als separate Parameter senden –
Wie erwarten Sie, dass es funktioniert? –