Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

c# - 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...