1

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);
        }
    }

2 Answers 2

1

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);
   }
Sign up to request clarification or add additional context in comments.

Comments

0

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.

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.