0

To start off with, I did try the information from several links including this one which I thought was the most useful. I am trying to make sure a user doesn't enter an email that is already in the database. I am trying to use the Remote attribute but from the code that I have, whenever I click the submit button, nothing happens. It's like what I have prevents the code from continuing.

Here is my code starting with part of the model. Employee.cs

    [Required(ErrorMessage ="Email Address is required.")]
    [Remote("checkEmailValidate", "Employee", ErrorMessage ="User with this Email already exists")]
    public virtual string EmailAddress { get; set; }

and here is part of my controller. EmployeeController.cs

    [AllowAnonymous]
    [HttpPost]
    public JsonResult checkEmailValidate(string email)
    {

        if (checkEmail(email))
        {
            return Json("Email Already Exists!", JsonRequestBehavior.AllowGet);
        }
        return Json(true, JsonRequestBehavior.AllowGet);
    }

    public Boolean checkEmail(string email)
    {

        Employee employee = (from e in db.Employees
                          where e.EmailAddress == email
                          select e).FirstOrDefault();
        Console.Write(employee.EmailAddress);
        if (employee == null)
        {
            return true;
        }


        return false;
    }

And here is a snippet from the view. I'm not sure whether I should put those scripts in there. I was trying to search around but I couldn't find a clear answer on that part, but I'm going to put it in here so you can get an idea of what I have.

    <div class="form-group">
        @Html.LabelFor(model => model.EmailAddress, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.EmailAddress, new { htmlAttributes = new { @class = "form-control", placeholder = "Email Address" } })
            @Html.ValidationMessageFor(model => model.EmailAddress, "", new { @class = "text-danger" })
        </div>
    </div>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

Any help would be welcome. Again when I click on the button to submit the new user's information, nothing seems to happen. What I want to have happen is to have some red text appear over or next to the box where they entered their email telling them that it already exists.

If there is an easier or "cheaper" way to have an alert like that popup on the same page so that it wont erase the other information that they put, that would be accepted as well. Thank you!

Community
  • 1
  • 1
Andrew McKeighan
  • 121
  • 1
  • 11

2 Answers2

1

Easiest steps : using Entity Framework and LINQ

get username data entered by user to the controller

string strUserName = Request["txtUserName"].ToString();

compare strUserName with Entity using linq

using (Entity _EntityObj = new Entity()) {
    var checkUserName = _EntityObj.Users.Where(x => x.UserName ==strUsername).FirstOrDefault();
}

checkUserName will return null if UserName does not exist

if(checkUserName != null) {
    // UserName available
}
else {
    // User Name exist
}
Meloman
  • 3,558
  • 3
  • 41
  • 51
Dip Surani
  • 61
  • 6
  • Can you please expand on this I'm sorry I'm not understanding. does all this code go in the checkEmailValidate action? – Moi Hawk Jan 28 '19 at 19:25
0

So it seems that my mistake was with the arguments for checkEmailValidate method. I am not sure if this is how it works, but it seems like i need to have the arguments variable name to be the same as it is in the controller. So with

public virtual string EmailAddress { get; set; }

I need to make sure that checkEmailValidate looked like this checkEmailValidate(string emailAddress) rather than checkEmailValidate(string email)

Andrew McKeighan
  • 121
  • 1
  • 11
  • 1
    Yes. The parameters to your POST methods need to match the `name` attribute on your HTML elements. – krillgar Dec 14 '16 at 17:02