سلام
چگونه می توانم از remote validation در DropDownList استفاده کنم
وقتی از در EditorFor استفده میکنم صحیح کارمیکند اما درDropDownList نه کارمیکند نه اورو میگیرد
چکار باید کنم
باتشکر
[Key, Column(Order = 1)] [StringLength(200)] [Display(Name = "MasterType")] [Remote("IsNameAvailable", "MasterSetUps", ErrorMessage = "Master type already exists ")] public string MasterType { get; set; }
<div class="form-group"> @Html.LabelFor(model => model.MasterType, htmlAttributes: new { @class = "control-label col-sm-2" }) <div class="col-sm-10"> @Html.DropDownList("MasterType", null, htmlAttributes: new { @class = "form-controls" }) @Html.ValidationMessageFor(model => model.MasterType, "", new { @class = "text-danger" }) </div> </div>
public ActionResult Create() { s_mode = "ADD"; ViewBag.MasterType = new SelectList(db.Masters, "MasterType", "MasterType"); return View(); }
سلام
استاد فرض کنید کدهای زیر را داریم
public partial class Student { public int StudentId { get; set; } public string FN { get; set; } public string LN { get; set; } [Remote("IsCheck", "Students", ErrorMessage = "Double")] public Nullable<int> CityId { get; set; } public virtual City City { get; set; } } public partial class City { public City() { this.Students = new HashSet<Student>(); } public int CityId { get; set; } public string Name { get; set; } public virtual ICollection<Student> Students { get; set; } }
@model Validation01.Models.Student @{ ViewBag.Title = "Create"; } <h2>Create</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Student</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.FN, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.FN, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.FN, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.LN, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.LN, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.LN, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.CityId, "CityId", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownList("CityId", null, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.CityId, "", 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> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
public ActionResult Create() { ViewBag.CityId = new SelectList(db.Cities, "CityId", "Name"); return View(); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "StudentId,FN,LN,CityId")] Student student) { if (ModelState.IsValid) { db.Students.Add(student); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.CityId = new SelectList(db.Cities, "CityId", "Name", student.CityId); return View(student); } public ActionResult IsCheck(string FN,int CityId) { var check = db.Students.FirstOrDefault(c => c.FN == FN && c.City.CityId == CityId); return Json(check!=null, JsonRequestBehavior.AllowGet); }
اما کدبالا عملیات چک را انجام نمیدهد
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)