【问题标题】:Some model properties always null on post, modelbinding issue某些模型属性在发布时始终为空,模型绑定问题
【发布时间】:2020-09-18 12:43:37
【问题描述】:

我正在制作一个网站的一部分,您可以在其中更新您的个人资料。该配置文件与作为 Microsoft 身份扩展的用户相关联。

问题LastNameFirstName在发出post请求后总是为空。 我想如果这是 ModelState 错误,我会通过 !ModelState.IsValid 发现它。

https://i.gyazo.com/eea1e3465427eba5a731947473fca821.mp4

型号

using Microsoft.AspNetCore.Identity;
using System;
namespace Certificate_Wiki.Models {
    public class CertificateUser : IdentityUser {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Website { get; set; }
        public string Occupation { get; set; }
        public string Country { get; set; }
        public string Description { get; set; }
        public String ProfilePictureUrl { get; set; }
        public Byte[] ProfilePicture { get; set; }
        public bool isPrivate { get; set; }
    }
}

cshtml

@{
    ViewData["Title"] = "Edit Profile";
}
@model Certificate_Wiki.Models.CertificateUser
<link rel="stylesheet" href="~/css/pages/ProfileEdit.css" media="all" />

<div class="background"></div>
<div class="content">
    <div class="content-image">
        <img src="~/images/profile/Component 1 – 1.png" alt="" />
    </div>
    <div class="content-profile">
        <div class="profile-image">
            <img src="https://www.pngitem.com/pimgs/m/78-786293_1240-x-1240-0-avatar-profile-icon-png.png" alt="error loading image" />
        </div>
        <div class="profile-form">
            <h2>@User.Identity.Name</h2>
            <div asp-validation-summary="All">
            </div>
            <form asp-action="Edit" method="post">

                <div class="form-row">
                    <label>First Name</label>
                    <input asp-for="FirstName" type="text" name="name" />
                </div>
                <div class="form-row">
                    <label>Last Name</label>
                    <input asp-for="LastName" type="text" name="name" />
                </div>
                <div class="form-row">
                    <label>Occupation</label>
                    <input asp-for="Occupation" type="text" name="occupation" />
                </div>
                <div class="form-row">
                    <label>Website</label>
                    <input asp-for="Website" type="url" name="website" />
                </div>
                <div class="form-row">
                    <label>Country</label>
                    <input asp-for="Country" type="text" name="Country" />
                </div>
                <div class="form-row">
                    <label>Profile Description</label>
                    <textarea asp-for="Description" type="text" name="description"></textarea>
                </div>
                <div class="form-row">
                    <label>Private Profile</label>
                    <input asp-for="isPrivate" type="checkbox" name="Private" />
                </div>

                <div class="form-row">
                    <button type="submit">Save</button>
                </div>
            </form>
        </div>
    </div>
</div>

控制器

[HttpGet]
[Authorize]
[Route("Profile/edit")]
public async Task<IActionResult> EditAsync() 
{
    var Profile = await userManager.FindByEmailAsync(User.Identity.Name);
    if (Profile == null) { return View(); }

    return View(Profile);
}

[ValidateAntiForgeryToken]
[HttpPost]
[Authorize]
[Route("Profile/edit")]
public async Task<IActionResult> EditAsync([FromForm]CertificateUser model) 
{
    //TODO
    //Remove CW from single-line if
    if (!ModelState.IsValid) { Console.WriteLine("Modelstate invalid"); return View(model); }

    var Profile = await userManager.FindByEmailAsync(User.Identity.Name);
    if (Profile == null) { return View(); }

    //Update database
    Profile.FirstName = model.FirstName;
    Profile.LastName = model.LastName;
    Profile.Description = model.Description;
    Profile.Country = model.Country;
    Profile.Occupation = model.Occupation;
    Profile.Website = model.Website;
    await userManager.UpdateAsync(Profile);

    Console.WriteLine("Update success");

    return RedirectToAction("Index");
}

注意,虽然我在这里,但我还想问是否有一种“更好”或更简洁的方式来更新用户,而无需 profile... = model... 来更新每个属性。

【问题讨论】:

    标签: asp.net .net asp.net-core asp.net-core-mvc asp.net-identity


    【解决方案1】:

    如果你对输入框使用 asp-for 属性,那么你不应该同时使用 name 属性(或者至少使用相同的值)。

    在您的示例中,您将 FirstName 设置为 asp-for,然后只为 FirstName 的输入的 name 属性设置 name。

    【讨论】:

    • 谢谢,成功了!在进行一些测试时,我将name="FirstName" 更改为name="name"。该复选框无关紧要,因为我还没有到达那部分哈哈。但是你对name="" vs asp-for=""有解释吗?
    • 如果你设置 asp-for 那么 name 属性也已经在后台设置为相同的值(在一些其他属性中),所以在这种情况下你不应该需要它(name 属性)完全没有。
    猜你喜欢
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多