比较属性不起作用

Posted

技术标签:

【中文标题】比较属性不起作用【英文标题】:Compare Attribute Not Working 【发布时间】:2015-12-07 12:07:46 【问题描述】:

我有密码并使用比较属性确认密码字段,但它有问题?尝试通过 NuGet 包管理器更新所有内容,即使我输入“123456”,密码仍然不匹配,不知道我该怎么办?

  public class AccountsViewModel
    
        public class Register
        
            [Required]
            public string Username  get; set; 

            [Required]
            [DataType(DataType.Password)]
            public string Password  get; set; 

            [Compare("Password")]
            [DataType(DataType.Password)]
            public string ConfirmPassword  get; set; 

        

    

控制器

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "id,username,password")] AccountsViewModel.Register viewModel)

    if (ModelState.IsValid)
    
        account account = new account();
        db.accounts.Add(account).username = viewModel.Username;
        db.accounts.Add(account).password = viewModel.Password;
        await db.SaveChangesAsync();
        return RedirectToAction("Index");
    

    return View(viewModel);

查看

@model trainingmvc.Models.AccountsViewModel.Register

@
    ViewBag.Title = "Create";


<h2>Create</h2>


@using (html.BeginForm("Create", "Accounts", FormMethod.Post, new  @class = "form-horizontal", role = "form" ))

    @Html.AntiForgeryToken()
    <h4>Create a new account.</h4>
    <hr />
    @Html.ValidationSummary("", new  @class = "text-danger" )
    <div class="form-group">
        @Html.LabelFor(m => m.Username, new  @class = "col-md-2 control-label" )
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.Username, new  @class = "form-control" )
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Password, new  @class = "col-md-2 control-label" )
        <div class="col-md-10">
            @Html.PasswordFor(m => m.Password, new  @class = "form-control" )
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.ConfirmPassword, new  @class = "col-md-2 control-label" )
        <div class="col-md-10">
            @Html.PasswordFor(m => m.ConfirmPassword, new  @class = "form-control" )
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Register" />
        </div>
    </div>


<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts 
    @Scripts.Render("~/bundles/jqueryval")

网页配置

   <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusivejavascriptEnabled" value="true" />

捆绑

  bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-version.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.validate*"));

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                      "~/Scripts/bootstrap.js",
                      "~/Scripts/respond.js"));

            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/bootstrap.css",
                      "~/Content/site.css"));

【问题讨论】:

关闭客户端验证,在回发的时候绑定PasswordConfirmPassword属性看看设置了什么。 很抱歉,我该怎么做?我对网络开发有点陌生。 它是否真的进入了服务器代码,还是在您调试时在客户端被拒绝?如果它到达服务器,可能是因为您没有绑定确认密码字段。尝试使 [Bind(Include = "id,username,password")] 这个 [Bind(Include = "id,username,password,confirmpassword")] 我在我的控制器上设置了一个断点,但它没有达到那个断点,所以我猜它在客户端失败了。 嗯。我的设置基本相同,没有问题。如果删除 datatype 属性并验证内容是否相同,是否仍会导致问题? 【参考方案1】:

你的 POST 方法有一个[Bind] 属性

public async Task<ActionResult> Create([Bind(Include = "id,username,password")] AccountsViewModel.Register viewModel)

从绑定中排除模型ConfirmPassword属性,因此它的值为null,这是无效的(它与Password的值不匹配)因此添加了ModelState错误。

AccountsViewModel 是一个视图模型,视图模型永远不需要Bind 属性(因为视图模型只包含在视图中编辑的属性)。只需删除该属性,以便绑定所有属性并且您的模型将有效。

【讨论】:

但是 OP 说 “我在控制器上设置了一个断点,但它没有达到那个断点,所以我猜它在客户端失败了”

以上是关于比较属性不起作用的主要内容,如果未能解决你的问题,请参考以下文章

为啥这个 DirectoryInfo 比较不起作用? [复制]

bootstrap table 表格太宽,设置的width属性不起作用怎么办

bootstrap table 表格太宽,设置的width属性不起作用怎么办

为啥从函数调用分配给属性 innerHTML 不起作用?

颤振平面按钮颜色属性不起作用

模型绑定属性属性不起作用