12

I have read a lot of step by step tutorials and still couldn't get my code to work, I went through the solutions on this site with no luck either, i don't know what i am doing wrong.

I am using jQuery and want to find out whether the username "mark" is taken or not, I haven't even reached the database linkage yet.

[HTML]

<input id="user_name" name="user_name" onchange="UserCheck()" type="text" value="" />
<div id="status" />

[JS]

function UserCheck() {
    $("#status").html("Checking....");
    $.post("/user/check",
    { username: $("#user_name").val() },
    function (data) {
        if (data == 0) {
            $("#status").html("Available");
        }
        else {
            $("#status").html("Taken");
        }
    });
}


[Controller]

public JsonResult check(FormCollection form)
{
    System.Threading.Thread.Sleep(3000);
    string name = form["username"];

    if (name.Equals("mark")){
        return Json(1);
    } else {
        return Json(0);
    }
}
4
  • 1
    you are using AJAX to do a post to the "check" method, this method is awaiting something that looks like FormCollection object, switch it to string instead like "JsonResult check(string name)" Commented Nov 20, 2011 at 9:11
  • provide more info about what is problem... is ajax call made ok? (use firebug to see this), is you action method called with it ? (use brakepoint) if yes, is there any server side error? Help us help you. Commented Nov 20, 2011 at 12:45
  • Try what @Joakim said. Also, add to your $.post an on error function and see if the request is working but the response is an error. The data might actually contain the 1 you were sending back but jQuery sees an error response and is therefor not calling your on success function. Commented Nov 20, 2011 at 14:40
  • Have you used fiddler or something similar to see what is returned when you make the ajax call? Is it even reaching the action on the server? Commented Nov 21, 2011 at 1:44

3 Answers 3

16

Check the below link out :

Check Instantly If Username Exists - ASP.NET MVC Remote Validation

What you need here is RemoteAttribute for the property your are checking and also you need to implement a controller action which returns JsonResult with Boolean value.

Here is a brief sample:

Your model:

    [Required]
    [Display(Name = "User name")]
    [Remote("doesUserNameExist", "Account", HttpMethod = "POST", ErrorMessage = "User name already exists. Please enter a different user name.")]
    public string UserName { get; set; } 

Your action result:

[HttpPost]
public JsonResult doesUserNameExist(string UserName) {

    var user = Membership.GetUser(UserName);

    return Json(user == null);
}

You can tweak the business logic inside the action result for your needs.

Also, make sure that you have the following libraries referenced on your registration page along with your jQuery file:

jquery.validate.min.js

jquery.validate.unobtrusive.min.js

The above blog post covers everything you need.

NOTE

Keep it in mind that Remote validation does not kick in on the server side. You might wanna check the below link out for server side remote validation (I don't recommend using it on production though, it is full of holes but it will give you an idea):

http://www.tugberkugurlu.com/archive/asp-net-mvc-server-side-remote-validation

Sign up to request clarification or add additional context in comments.

7 Comments

Isn't there a way without using Remote Attribute? Basically making my code work somehow.
of course there is. Manually make ajax calls to your controller action. But it requires lot of work for you: you need to check if this is working with ok with your current validation system, validation display logic, etc.
@Cindro there is no way for that not to be working. Full code of the project is on GitHub: github.com/tugberkugurlu/MvcRemoteValidationSample check that out. You probably doing something wrong.
I got it to work, BUT I would like to know why my code wasn't working, what is what I was mainly interested in, i preferred my clean code. Why wasn't my code working?
@Cindro are you sure that your ajax call is coming to your controller action smoothly? start a debug session and put a breakpoint on check method to see what you are getting. Also, inspect the your calls using something like fiddler to see what you send and get.
|
3

See my full article on this topic. How to: Implement Remote Validation in ASP.NET MVC

Comments

0

Go through these links:

http://www.dotnetfunda.com/articles/show/2893/checking-for-existing-username-without-page-refresh-in-aspnet-mvc-remo

http://www.tugberkugurlu.com/archive/check-instantly-if-username-exists-asp-net-mvc-remote-validation

https://msdn.microsoft.com/en-us/library/gg508808%28v=VS.98%29.aspx

Using JQuery

https://2leggedspider.wordpress.com/2009/08/17/check-availability-of-username-using-asp-net-mvc-and-jquery/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.