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

4
  • 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. Commented Jul 17, 2017 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. Commented Jul 18, 2017 at 2: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 Commented Feb 15, 2018 at 0:19
  • The dupe explains it. Commented Feb 15, 2018 at 0:24

2 Answers 2

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);
            }
        }
Sign up to request clarification or add additional context in comments.

2 Comments

how do you use the id ?
@UmarAftab if you create the record then will goes 0 and for edit mode pass the actual and add the logic for you lamda expression.
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 !

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.