简体   繁体   中英

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.

<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

      [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

If the two passwords are in your Userdata model,you need use Userdata.Password1 and Userdata.Password2 in frontend:

<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]
public String Password1 { get; set; }
[BindProperty]
public String Password2 { get; set; }

Result:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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