gpt4 book ai didi

jquery - 在MVC 3.0中显示RemoteAttribute的结果

转载 作者:行者123 更新时间:2023-12-01 07:32:18 25 4
gpt4 key购买 nike

我有一个 ViewModel 设置,可以通过 RemoteAttribute 使用 RemoteValidation。效果很好。

编辑

Updated it a bit to show some fixed code.

I want to point out that this is not my actual "Register" code. This is testing it so I can use it in other situations. I'm not having users register using flat names!

以下是我引用的库,以及我如何引用它们。

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

这是实际的 Controller 行为。

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

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

return Json(false, JsonRequestBehavior.AllowGet);
}

我已经检查了我的 HTML,它的功能符合我的要求。但我不明白从那里该做什么。我如何向用户显示该信息?它只是将其存储在 html 中

data-val-remote="* 无效"

我观察过,我注意到即使 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; }
}

Controller :

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com