arithehun
arithehun

Reputation: 25

ASP.NET MVC Remote Validation: Return boolean and string

I have been attempting to use ASP.NET MVC remote validation for username login access to return both a string and a boolean. If I return just a string, it will assume a boolean of false and not let the user submit the form. Is there a way I could pass in both true and a string such as "[USERNAME] is available!"?

Current method:

public JsonResult isUserAvailable(string username)
    {
        if (Membership.GetUser(username) == null)
        {
            return Json(String.Format(CultureInfo.InvariantCulture, "<strong style='color: green;'>{0} is available!</strong>", 
                username), JsonRequestBehavior.AllowGet);
        }

        else
        {
            return Json(false, JsonRequestBehavior.AllowGet);
        }
    }

Upvotes: 1

Views: 826

Answers (2)

Kane
Kane

Reputation: 16802

As @Furqan suggested the data you are returning is not Json but Html.

I suggest that you either return a Json object or a different ActionResult, perhaps ContentResult. This blog post does a good job of explaining the different ActionResult types.

Upvotes: 0

Furqan Hameedi
Furqan Hameedi

Reputation: 4400

Use Json object to send the data back, modify your else block like this and consume JSON on client

 else
    {
       var data = new 
       { 
         result = false, 
         userName = username 
       };
 return Json(data, JsonRequestBehavior.AllowGet);
   }

Upvotes: 1

Related Questions