مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

iran

عضویت از 1394/01/31

نحوه استفاده remote validation در DropDownList

  • دوشنبه 7 خرداد 1397
  • 18:41
تشکر میکنم

سلام

چگونه می توانم از remote validation در  DropDownList استفاده کنم

وقتی  از در EditorFor استفده میکنم صحیح کارمیکند اما درDropDownList  نه کارمیکند نه اورو میگیرد 

چکار باید کنم

باتشکر

پاسخ های این پرسش

تعداد پاسخ ها : 2 پاسخ
کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • سه شنبه 8 خرداد 1397
  • 09:15
 [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();
}

کاربر سایت

iran

عضویت از 1394/01/31

  • سه شنبه 8 خرداد 1397
  • 12:28

سلام

استاد فرض کنید کدهای زیر را داریم

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);
        }

اما کدبالا عملیات چک را انجام نمیدهد

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)