Mapping RemoteAttribute Result in MVC 3.0

I have a ViewModel setup to use RemoteValidation with RemoteAttribute. It works great.

EDIT

Updated it a bit to show fixed code.

I want to point out that this is not my actual "Registration" code. This tests it, so I can use it in other situations. I do not have user registration using flat names!

Here are the libraries that I refer to, and how I refer to them.

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.js"></script>

<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.unobtrusive-ajax.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js"></script>

This is how I connect RemoteAttribute.

public class UserRegistrationModel
{
    [Required]
    [RegularExpression(@"^(?:[a-zA-Z\p{L} \.'\-]{3,48})$", ErrorMessage = "This name contains invalid characters. Names must be between 3 and 48 characters, contain only standard unicode symbols, and may not contain any punctuation other than the ['] and [-] symbols.")]
    [Remote("ValidateUserName", "Membership", ErrorMessage = "{0} is invalid.")]
    public string Name
    {
        get;
        set;
    }
}

And here is the actual behavior of the controller.

    public ActionResult ValidateUserName(string name)
    {
        // perform logic

        if (true)
            return Json(true, JsonRequestBehavior.AllowGet);

        return Json(false, JsonRequestBehavior.AllowGet);
    }

I checked my HTML and it works the way I want. But I do not understand what to do next. How can I display this information for the user? It just stores it in html

data-val-remote="* is invalid"

, RemoteAttribute false, html

value value class="valid", `class= " -".

- , ? ?

+1
1

:

public class UserRegistrationViewModel
{
    [Required]
    [RegularExpression(@"^(?:[a-zA-Z\p{L} \.'\-]{3,48})$", ErrorMessage = "This name contains invalid characters. Names must be between 3 and 48 characters, contain only standard unicode symbols, and may not contain any punctuation other than the ['] and [-] symbols.")]
    [Remote("ValidateUniqueName", "Home")]
    public string Name { get; set; }
}

:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new UserRegistrationViewModel());
    }

    public ActionResult ValidateUniqueName(string Name)
    {
        if (NameIsValid(Name)) 
        {
            return Json(true, JsonRequestBehavior.AllowGet);
        }

        return Json(string.Format("{0} is invalid", Name), JsonRequestBehavior.AllowGet);
    }
}

:

@model AppName.Models.UserRegistrationViewModel

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

@using (Html.BeginForm())
{
    @Html.TextBoxFor(x => x.Name)
    @Html.ValidationMessageFor(x => x.Name)
    <input type="submit" value="OK" />
}

.

+4

All Articles