MVC中的远程验证受影响的编辑 [英] remote validation in mvc affected edit

查看:82
本文介绍了MVC中的远程验证受影响的编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过客户端的远程验证来验证用户名,并且在创建模块中添加重复字段时可以正常工作,但是现在不允许我使用相同的名称编辑记录,这向我显示了相同的错误我为创建定义的.我尝试了所有可能的方法,但没有成功,请帮助我.我已经遵循了这些链接,但是无论哪种方式都无法正常工作.

I was trying to validate the user name through remote validation in client side and it's working fine in while adding the duplicate field in create Module but now it is not allowing me to edit the record using same name it's showing me the same error which I defined for create. I tried all the possible ways but not succeeded please help me. I have followed these link but it's not working in either way.

http://stackoverflow.com/questions/4778151/asp-net-mvc-3-remote-validation-to-allow-original-value http://stackoverflow.com/questions/6407096/asp-net-mvc-3-remote-attribute-passing-3-fields

http://stackoverflow.com/questions/4778151/asp-net-mvc-3-remote-validation-to-allow-original-value http://stackoverflow.com/questions/6407096/asp-net-mvc-3-remote-attribute-passing-3-fields

这是到目前为止我尝试过的代码.请帮助专家.

here is my code what i have tried so far .please help experts.

[Required]
        [Remote("IsUserAvailable", "User", HttpMethod = "Post", ErrorMessage = "User already exist.", AdditionalFields = "InitialUserName")]
        [RegularExpression(@"^(?![\W_]+$)(?!\d+$)[a-zA-Z0-9 ]+$", ErrorMessage = "Invalid UserName ")]
 public string UserName { get; set; }


[HttpPost]
    public JsonResult IsUserAvailable([Bind(Prefix = "User.UserName")]string UserName, string initialUserName)
    {

        var result = uDbContext.Users.FirstOrDefault(a => a.UserName == UserName);
        if (result == null)
        {
            return Json(true, JsonRequestBehavior.AllowGet);
        }
        return Json(JsonRequestBehavior.AllowGet);
    }


@model User.ViewModel.ViewModelUser
@using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        @Html.HiddenFor(m => m.User.UserId)



                @Html.LabelFor(m.User.UserName)


               @Html.TextBoxFor(m => m.User.UserName)
                @Html.ValidationMessageFor(m.User.UserName)
                @Html.Hidden("initialUserName", Model.User)
            </div>
        </div>
}

请帮助专家完成我的任务.

Please help experts to complete my assignment.

推荐答案

User似乎是一个复杂的对象,所以

User appears to be a complex object so

@Html.Hidden("initialUserName", Model.User)

可能会生成类似

<input type="hidden" name="initialUserName" value="YourAssemly.User" ... />

这对验证没有帮助.

您可以通过使用发回原始名称来忽略验证

You could ignore the validation by sending back the original name using

@Html.Hidden("InitialUserName", Model.User.UserName)

@Html.Hidden("User.InitialUserName", Model.User.UserName)

,然后使用

public JsonResult IsUserAvailable([Bind(Prefix = "User.UserName")]string UserName, string initialUserName)

public JsonResult IsUserAvailable([Bind(Prefix = "User.UserName")]string UserName, [Bind(Prefix = "User.InitialUserName")]string initialUserName)
{
  if (UserName == initialUserName)
  {
    // Nothing has changed so signal its valid
    return Json(true, JsonRequestBehavior.AllowGet);
  }
  // Check if the user name already exists
  var result = uDbContext.Users.FirstOrDefault(a => a.UserName == UserName);
  return Json(result == null, JsonRequestBehavior.AllowGet);
}

侧面说明:jQuery远程验证是一个GET调用,因此不需要[HttpPost]属性

Side note: jquery remote validation is a GET call so the [HttpPost] attribute is not necessary

修改

在调试jquery-validate.jsjquery-validate-unobtrusive.js文件之后,事实证明,任何AdditionalFields的name属性都必须包含与要验证的属性相同的前缀,然后还需要[Bind(Prefix="..")]属性关于方法中的那些参数(请参阅上面的修订)

After debugging both the jquery-validate.js and jquery-validate-unobtrusive.js files, it turns out that the name attribute of any AdditionalFields must include the same prefix as the property being validated, and that the [Bind(Prefix="..")] attribute is then also required on those parameters in the method (refer amendments above)

例如,另一种方法可能是创建一个简单的类以发布回去

An alternative might to create a simple class to post back to, for example

public class ValidateUserNameVM
{
  public string UserName { get; set; }
  public string InitialUserName { get; set; }
}

public JsonResult IsUserAvailable([Bind(Prefix = "User")]ValidateUserNameVM model)
{
  if (model.UserName == model.InitialUserName)
  ....

这篇关于MVC中的远程验证受影响的编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆