سلام
من کلاس و کنترلر و view زیر را نوشته ام
using System.ComponentModel.DataAnnotations; namespace ErrorClient.Models { public class Login { public int Id { get; set; } [Required(ErrorMessage = "number is Required...")] [RegularExpression("^[0-9]{5}$", ErrorMessage = "Not number")] public string User { get; set; } [Required(ErrorMessage = "string is Required...")] [RegularExpression("^[a-z]{5}$", ErrorMessage = "Not string")] public string Pass { get; set; } } }
public ActionResult Create() { return View(); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "Id,User,Pass")] Login login) { if (ModelState.IsValid) { db.Logins.Add(login); db.SaveChanges(); return RedirectToAction("Index"); } return View(login); }
@model ErrorClient.Models.Login @{ ViewBag.Title = "Create"; } @section ValidateCss { <link href="~/Content/jquery.validate.css" rel="stylesheet" /> } @section ValidateJquery { <script src="~/Scripts/jquery.validatevalidateError.js"></script> <script> $(function () { $("#myform").validate(); }); </script> } <h2>Create</h2> @using (Html.BeginForm("Index", "Logins", FormMethod.Post, new { id = "myform" })) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Login</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.User, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.User, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.User, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Pass, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Pass, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Pass, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div>
اما Validation سمت سرور کار نمی کند
اما وقتی خط
@using (Html.BeginForm("Index", "Logins", FormMethod.Post, new { id = "myform" }))
را به تبدیل به کد زیر میکنم
@using (Html.BeginForm())
Validation سمت سرور کار می کند
برای حل این مشکل باید چکار کنم
باتشکر
به احتمال زیاد برای id که دادید
با اون id کاری ajaxi انجام میدید ؟
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)