Search code examples
asp.netasp.net-mvc-4razorremote-validation

Remote validation restrict for edit controller method


I have model for register

my model class

public class RegisterViewModel
{


    [Required]
    [StringLength(100, ErrorMessage = "Please enter a minimum of {2} characters", MinimumLength = 6)]
    [DisplayName("University ID")]
    [Remote("doesusernameExist", "HEC",null, ErrorMessage = "usr name is allready exist", HttpMethod = "POST")]
    public string usrname { get; set; }    }

my json controller class

 [HttpPost]
    public JsonResult doesusernameExist(string usrname)
    {

         var institute = db.Institutes.Find(HEI_ID);
                   return Json(institute == null);


    }

for create new user and edit user I'm using above model . so without create another model , I want to disable doesusernameExist calling method in edit method


Solution

  • First in Edit View disable client side validation for username:

    @Html.TextBoxFor(m => m.username, new { @data_val = "false" })
    

    Second in Edit Post Action remove validation result for username from ModelState:

    public ActionResult EditUser([Bind(Exclude = "usrname")]RegisterViewModel model)
    {
        ModelState.Remove("username");
        if (ModelState.IsValid)
        {
        .
        .
        .