【问题标题】:Validation Existing Data with Create and Edit Action使用创建和编辑操作验证现有数据
【发布时间】:2014-06-10 00:41:07
【问题描述】:

我是 ASP.NET 和 MVC 编程的初学者。我有一个问题(我的标题)。 首先,我构建了 Create 动作。按照链接: http://www.tugberkugurlu.com/archive/check-instantly-if-username-exists-asp-net-mvc-remote-validation.

但是这样,我的编辑操作将检查并获取有关现有数据的错误。 我的问题是如何在使用编辑操作时跳过检查现有数据。 感谢您的帮助。

【问题讨论】:

  • 为动作名称取另一个参数,如果是编辑则不要签入数据库
  • @Ehsan 它将如何解决问题?模型由[Remote("doesUserNameExist", "Account", HttpMethod = "POST", ErrorMessage = "User name already exists. Please enter a different user name.")] 为用户名组成,模型将在任何地方被调用,用于检查用户名的 JSON 操作将被触发。当用户在注册时输入后无权编辑其凭据时使用此选项。

标签: c# asp.net asp.net-mvc


【解决方案1】:

从模型中删除此代码:

[Remote("doesUserNameExist", "Account", HttpMethod = "POST", ErrorMessage = "User name already exists. Please enter a different user name.")]

AJAX 方法:

$(document).ready(function() {
$("#btnCheck").click(function() {
    var name = $("#UserName").val(); //Value entered in the text box
    var status = $("#divStatus"); //DIV object to display the status message
    status.html("Checking....") //While our Thread works, we will show some message to indicate the progress

    //jQuery AJAX Post request
    $.post("/Account/doesUserNameExist", { username: name },
        function(data) {
            if (data == "true") {
                status.html(name + " is available!");
            } else {
                status.html(name + " is not available!");
            }
        });
});
});

模型组成 [Remote("doesUserNameExist", "Account", HttpMethod = "POST", ErrorMessage = "User name already exists. Please enter a different user name.")] 用于 UserName 以及将调用模型的任何位置都会触发用于检查用户名的 JSON 操作。

当用户在注册时输入后无权编辑其凭据时使用。

【讨论】:

  • 你不能这样做。我的创建操作必须检查存在的用户名。有什么理想吗?
  • 是的,为此您可以使用 javascript 确保仅在创建而不是编辑时检查检查用户
  • 如果你会在模型中提到Check函数,那么每次调用模型时都会检查
  • 使用 AJAX 调用动作
  • 我看到了你的评论。谢谢你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
相关资源
最近更新 更多