简体   繁体   English

通过 asp-for="..." 将字符串值传递到 ASP.Net 核心中的 Model 无法正常工作

[英]Passing a String value through asp-for=“…” into a Model in ASP.Net core isn't working the way it should

So i try to pass a password type of String by sending the Form, all the other data is working the way it should but when i try the same thing with the 2 passwords there just happens nothing.因此,我尝试通过发送表单来传递字符串类型的密码,所有其他数据都按应有的方式工作,但是当我使用 2 个密码尝试相同的操作时,什么也没有发生。

<hr />
<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Userdata.Username" class="control-label"></label>
                <input asp-for="Userdata.Username" class="form-control" required />
                <span asp-validation-for="Userdata.Username" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Password1" class="control-label">Password</label>
                <input asp-for="Password1" class="form-control" type="password" required />
                <span asp-validation-for="Password1" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Password2" class="control-label">Password</label>
                <input asp-for="Password2" class="form-control" type="password" required />
                <span asp-validation-for="Password2" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Userdata.Role" class="control-label"></label>
                <input asp-for="Userdata.Role" class="form-control" required />
                <span asp-validation-for="Userdata.Role" class="text-danger"></span>
            </div>`
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

The upper Code is from the csHTML上面的代码来自csHTML

      [BindProperty]
        public Userdata Userdata { get; set; }

        public String Password1 { get; set; }
        public String Password2 { get; set; }


        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task<IActionResult> OnPostAsync()
        {

            if (!ModelState.IsValid)
            {
                return Page();
            }

            if (Password1 != Password2)
            {
                errormessage = "Passwords don't Match";
                return null;
            }

The upper code is from the Model class where i try to check if the Passwords match but they keep beeing Null上面的代码来自 Model class 我尝试检查密码是否匹配但它们一直在蜂鸣 Null

If the two passwords are in your Userdata model,you need use Userdata.Password1 and Userdata.Password2 in frontend:如果两个密码在您的 Userdata model 中,您需要在前端使用Userdata.Password1Userdata.Password2

<div class="form-group">
     <label asp-for="Userdata.Password1" class="control-label">Password</label>
     <input asp-for="Userdata.Password1" class="form-control" type="password" required />
     <span asp-validation-for="Userdata.Password1" class="text-danger"></span>
</div>
<div class="form-group">
     <label asp-for="Userdata.Password2" class="control-label">Password</label>
     <input asp-for="Userdata.Password2" class="form-control" type="password" required />
     <span asp-validation-for="Userdata.Password2" class="text-danger"></span>
</div>

If you just want receive data for the following property:如果您只想接收以下属性的数据:

public String Password1 { get; set; }
public String Password2 { get; set; }

You need add [BindProperty] attribute on them:您需要在它们上添加[BindProperty]属性:

[BindProperty]
public String Password1 { get; set; }
[BindProperty]
public String Password2 { get; set; }

Result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM