I need to validate one input is unique or not before inserting to DB. I used remote validation attribute in my model class.
[Required(ErrorMessage = "Package name Required")]
[Remote("IsNameAvailble", "Package", ErrorMessage = "Sorry!!! This name have entered for another package")]
public string packagename { get; set; }
View
<div class="form-group">
<label class="control-label">Package Name</label>
@Html.TextBoxFor(model => model.package_name, new { @class = "form-control", placeholder = "Name for the Package", type = "text", autofocus = "autofocus", id = "packagename" })
@Html.ValidationMessageFor(model => model.package_name)
</div>
Controller code
public ActionResult IsNameAvailble(string package_name)
{
Dbfile db = new Dbfile ();
var exist= db.GetAllList().FirstOrDefault(m => m.package_name == package_name);
if (exist!= null)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
This is working perfect in Adding a new name but in editing part if we changing other values (other than package name , it check and display error message )
so I need a way to check it with selected id (if we check with selected id, thn create function won't work )
please anyone suggest answer
Thanks in Advance
What I have tried:
here if I used AdditionalFields property of [Remote] attribute to pass ID , it working in edit view only .