0

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>

This is 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

kAsdh
  • 356
  • 1
  • 10
  • 25
  • The question isn't all that clear. you need a way to check 'it' with selected id. Please use complete sentences. Just guessing here... When you are editing, pass the Id into the view, then take the id from view when you submit. Use `db.Find(id)` to check if an entry exists. – lloyd Jul 17 '17 at 20:33
  • You need to use the `AdditionalFields` property of `[Remote]` attribute to also pass the ID value to rhe method so that your can exclude the current object from your query. –  Jul 18 '17 at 02:48
  • @StephenMuecke How is the AdditionalFields property used, lets say if it is an email, can you give a quick description or give me a reference point to implement it – Umar Aftab Feb 15 '18 at 00:19
  • The dupe explains it. –  Feb 15 '18 at 00:24

2 Answers2

1

Step 1 :- Add the AdditionalFields in remote attribute.

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

Step 2 :- pass the id parameter in IsNameAvailble method and also add the condition through which can we check if the id is greater then zero then lamda expression should check the record with id and name otherwise just check name.

public ActionResult IsNameAvailble(string package_name,int id=0)
        {
            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);
            }
        }
Shahzad Khan
  • 432
  • 2
  • 14
0

For getting this done you need to change your Remote Attribute by following :

[Remote("IsNameAvailble", "Package", HttpMethod = "POST", AdditionalFields = nameof(PackageId), ErrorMessage = "Sorry!!! This name have entered for another package.")]

And also you need to change your method signature with below :

public ActionResult IsNameAvailble(YourModel modelData)
{
    // You can access PackageName and PackageId by modelData.PackageName and 
    // model.PackageId
    // Also you can check if Id already exist

}

This way you can solve your edit view problem !

Nikunj Patel
  • 249
  • 3
  • 13