我有一个ViewModel设置,可以使用RemoteAttribute使用RemoteValidation。工作正常。

编辑


更新了一点以显示一些固定代码。

我想指出的是,这不是我的实际“注册”代码。这是测试它,因此我可以在其他情况下使用它。我没有用户使用统一名称注册!


这是我正在引用的库,以及我如何引用它们。

<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>


这是我连接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;
    }
}


然后是实际的控制器行为。

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

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

        return Json(false, JsonRequestBehavior.AllowGet);
    }


我已经检查了我的HTML,并且可以根据需要运行它。但是我不知道该怎么办。如何向用户显示该信息?它只是将其存储在html中

data-val-remote="* is invalid"

我已经看过了,我注意到即使RemoteAttribute返回false,html也会从

value转换为value class="valid",但在我的其他模型验证中,此更改为`class =“ input-validation-error”'。

那么,有人对如何回传远程消息有任何线索吗?还是真的无能为力?

最佳答案

以下对我有用:

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" />
}


您可能还会发现following blog post有用。

关于jquery - 在MVC 3.0中显示RemoteAttribute的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5121160/

10-10 00:04