0

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

Chathz
  • 723
  • 4
  • 16
  • 41
  • 1
    You either need 2 view models (best solution), or you need to pass an additional value to the `doesusernameExist()` method (say the ID property) using the `AdditionalFields` property of `RemoteAttribute` so that you can use that to conditionally test if the database check needs to be performed (i.e. if the ID is null or zero its a new object, so `return Json(true);` or whatever logic you want to apply) –  Apr 06 '15 at 12:23

2 Answers2

2

First Add a Hidden field in View as:

@Html.hidden("PreviousUsername", Model.usrname)

In your .cs class add following:

[Remote("doesusernameExist", "HEC", ErrorMessage = "usr name is allready exist", AdditionalFields = "PreviousUsername")]
public string usrname { get; set; }

An your controller Action Method should be like:

public JsonResult doesusernameExist(string usrname, string PreviousUsername)
{

     if(usrname==PreviousUsername)
     {
       return true;
     }
     var institute = db.Institutes.Find(HEI_ID);
     return Json(false, JsonAlloBehaviour.AllowGet);

}
Jamil Moughal
  • 19
  • 1
  • 6
1

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)
    {
    .
    .
    .
Mohsen Esmailpour
  • 11,224
  • 3
  • 45
  • 66
  • This removes all validation including the `[Required]` and `[StringLength]` validation! –  Apr 07 '15 at 23:27
  • 1
    Of course it removes all validations but in `Edit User Action` there is no need to check username because it's already exists and user cannot change his/her username. it's obvious, isn't it ? – Mohsen Esmailpour Apr 08 '15 at 04:36
  • You have shown a textbox so the user can edit it. So the user clears the contents of the textbox or edits it to more than 100 characters or edits it to be the same as some other users 'username', all of which are invalid but wont be picked up. –  Apr 08 '15 at 22:52
  • If look at the answer carefully `[Bind(Exclude = "usrname")]` solve this problem. – Mohsen Esmailpour Apr 08 '15 at 23:47