Click here to Skip to main content
14,216,096 members
Rate this:
Please Sign up or sign in to vote.
// In my home controller
[HttpGet]
public JsonResult IsUserNameExist(string Name)
{
using (CobraDBEntities db = new CobraDBEntities())
{
var check = !db.Person.Any(x => x.Name == Name);// if we use Person per then per.Name, and so on but we use string Name so that

return Json(check, JsonRequestBehavior.AllowGet);
}
}

// Annotation Required Field Validator Class
public  class PersonMD
      {
          public int Id { get; set; }
          [Required]
        // using json look in home controller
          [Remote("IsUserNameExist", "Home",ErrorMessage ="  Username Already Exist Use Another one")]
          public string Name { get; set; }
          [Required]
          public string Address1 { get; set; }
          [Required]
          public string Address2 { get; set; }
          [Required]
          public string Town_City { get; set; }
      }


Without using Angularjs this is working fine
If i use angularjs then
IsUserNameExist
doesn't work

What I have tried:

// in index.cshtml
<div class="modal-body">
                        <form name="form1">
                            <table class="table">
                                <tr>
                                    <td>Name</td>
                                    <td>
                                        <input type="text" ng-model="Name" name="name" ng-minlength="3" ng-maxlength="20" required />
                                        @*<span color="alert-danger" class="help-block" ng-show="form1.name.$invalid">Required </span>*@
                                        <span color="red" class="label label-info" ng-show="form1.name.$error.minlength">Too short </span>
                                        <span color="alert-danger" class="label label-info" ng-show="form1.name.$error.maxlength">Too long </span>
                                    </td>
                                <tr>
                                    <td>Address1</td>
                                    <td>
                                        <input type="text" ng-model="Address1" name="Add1" ng-minlength="3" ng-maxlength="100" required />
                                      
                                        <span color="alert-danger" class="label label-info" ng-show="form1.Add1.$error.minlength">Too short </span>
                                        <span color="alert-danger" class="label label-info" ng-show="form1.Add1.$error.maxlength">Too long </span>
                                    </td>
                                </tr>
                                <tr>
                                    <td>Address2</td>
                                    <td>
                                        <input type="text" ng-model="Address2" name="Add2" ng-minlength="3" ng-maxlength="100" required />
                                       
                                        <span color="alert-danger" class="label label-info" ng-show="form1.Add2.$error.minlength">Too short </span>
                                        <span color="alert-danger" class="label label-info" ng-show="form1.Add2.$error.maxlength">Too long </span>
                                    </td>
                                </tr>
                                <tr>
                                    <td>[Town/City]</td>
                                    <td>
                                        <input type="text" ng-model="Town_City" name="town" ng-minlength="3" ng-maxlength="20" required />
                                       
                                        <span color="alert-danger" class="label label-info" ng-show="form1.town.$error.minlength">Too short </span>
                                        <span color="alert-danger" class="label label-info" ng-show="form1.town.$error.maxlength">Too long </span>
                                        
                                    </td>
                                </tr>
                                <tr>

                                    <td>
                                        <input type="button" class="btn btn-default" value="Save" ng-disabled="form1.$invalid" data-dismiss="modal" ng-click="AddUpdatePerson()" />
                                        <input type="button" class="btn btn-danger" value="Cancel" data-dismiss="modal" ng-click="Cancel()" />
                                    </td>
                                </tr>
                            </table>
                        </form>

                    </div>
Posted
Comments
Nathan Minier 8-Mar-17 8:03am
   
This looks like you're trying to do client-side validation with rules from the Model class, not server-side validation, is that right?
Member 12999575 12-Mar-17 15:42pm
   
I want to do both ,
client side is working fine but not server side.
My main intension is to prevent dublication of emailid or username ?
When i type on client side form it should infrom as fast as possible ? before using Angularjs I used to do by using Json which was working fine but after using Angularjs that was not working anymore?

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



Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190619.2 | Last Updated 7 Mar 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100