显示远程验证成功响应的自定义消息

发布于 2024-11-16 05:12:32 字数 556 浏览 3 评论 0原文

我正在使用远程验证来检查我的 asp.net mvc 3 应用程序 (C#) 注册期间用户名的可用性。

我使用 MVC 远程属性验证作为:

[Remote("IsUserNameAvailable", "User")]
public string UserName { get; set; }

我需要在两个条件下显示消息:

  1. 显示错误消息“用户名不可用” - 失败条件
  2. 显示成功消息“用户名可用” - 成功条件

我能够显示失败条件的消息,而无需任何问题,如:

return Json("Username not available", JsonRequestBehavior.AllowGet);

但对于成功条件,我需要发送 true 响应(不使用自定义消息)为:

 return Json(true, JsonRequestBehavior.AllowGet);

如何显示远程验证成功条件的自定义消息?

I am using remote validation to check the availability of username during registration for my asp.net mvc 3 applicaion (C#).

I am using MVC remote Attribute validation as:

[Remote("IsUserNameAvailable", "User")]
public string UserName { get; set; }

I need to show the message on two conditions:

  1. Show error message "Username not available" - Failure Condition
  2. Show Success message "Username available" - Success Condition

I am able to show the Failure Condition's message without any issue like:

return Json("Username not available", JsonRequestBehavior.AllowGet);

But for Success Condition, I need to send true in response(not with the custom message) as:

 return Json(true, JsonRequestBehavior.AllowGet);

How can i show custom message for Success Condition of Remote validation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听不够的曲调 2024-11-23 05:12:32

看到这个链接...
此处

一种方法实现的方法是从验证操作中添加自定义 HTTP 响应标头:

public ActionResult IsUserNameAvailable(string username)
{
if (IsValid(username))
{
    // add the id that you want to communicate to the client
    // in case of validation success as a custom HTTP header
    Response.AddHeader("X-ID", "123");
    return Json(true, JsonRequestBehavior.AllowGet);
}

return Json("The username is invalid", JsonRequestBehavior.AllowGet);
}

现在在客户端上,我们显然有一个标准表单和一个用户名输入字段:

@model MyViewModel
@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.UserName)
    @Html.ValidationMessageFor(x => x.UserName)
    <button type="submit">OK</button>
}

现在最后一部分难题是将完整的处理程序附加到用户名字段上的远程规则:

$(function () {
$('#UserName').rules().remote.complete = function (xhr) {
    if (xhr.status == 200 && xhr.responseText === 'true') {
        // validation succeeded => we fetch the id that
        // was sent from the server
        var id = xhr.getResponseHeader('X-ID');

        // and of course we do something useful with this id
        alert(id);
    }
};
});

see this link...
here

One way to achieve that is to add a custom HTTP response header from the validation action:

public ActionResult IsUserNameAvailable(string username)
{
if (IsValid(username))
{
    // add the id that you want to communicate to the client
    // in case of validation success as a custom HTTP header
    Response.AddHeader("X-ID", "123");
    return Json(true, JsonRequestBehavior.AllowGet);
}

return Json("The username is invalid", JsonRequestBehavior.AllowGet);
}

Now on the client we obviously have a standard form and an input field for the username:

@model MyViewModel
@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.UserName)
    @Html.ValidationMessageFor(x => x.UserName)
    <button type="submit">OK</button>
}

and now the last piece of the puzzle is to attach a complete handler to the remote rule on the username field:

$(function () {
$('#UserName').rules().remote.complete = function (xhr) {
    if (xhr.status == 200 && xhr.responseText === 'true') {
        // validation succeeded => we fetch the id that
        // was sent from the server
        var id = xhr.getResponseHeader('X-ID');

        // and of course we do something useful with this id
        alert(id);
    }
};
});
香草可樂 2024-11-23 05:12:32

你能返回一个对象(将被序列化为 Json)吗?

如:

var answer = new { success = true, message = "Username available" };
return Json(answer, JsonRequestBehavior.AllowGet);

那么就可以在视图中解析这个了。

另外,如果您这样做,但用户名不可用,您也可以添加一些建议的用户名。

例如

// pretend they chose "dave"
List<string> alternativeNames = new List<string>() { "dave1", "dave2" };
var answer = new { success = false, message = "Username not available", alternatives = alternativeNames };
return Json(answer, JsonRequestBehavior.AllowGet);

Are you able to return an object (which will be serialised to Json)?

Such as:

var answer = new { success = true, message = "Username available" };
return Json(answer, JsonRequestBehavior.AllowGet);

Then you can parse this in the view.

Also, if you do it this way, but the username is NOT available, you could add a few suggested usernames too.

e.g.

// pretend they chose "dave"
List<string> alternativeNames = new List<string>() { "dave1", "dave2" };
var answer = new { success = false, message = "Username not available", alternatives = alternativeNames };
return Json(answer, JsonRequestBehavior.AllowGet);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文