Click here to Skip to main content
15,029,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to validate one input is unique or not before inserting to DB. I used remote validation attribute in my model class.

C#
[Required(ErrorMessage = "Package name Required")]
[Remote("IsNameAvailble", "Package", ErrorMessage = "Sorry!!! This name have entered for another package")]
public string packagename { get; set; }


View
CSS
<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

C#
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 .
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900