代码之家  ›  专栏  ›  技术社区  ›  Stanley

一些模型属性在post、modelbinding问题上始终为空

  •  0
  • Stanley  · 技术社区  · 4 年前

    我正在做一个网站的一部分,你可以更新你的个人资料。配置文件绑定到作为Microsofts标识扩展的用户。

    问题 是吗 LastName FirstName 发出post请求后始终为空。 我想如果这是一个 ModelState 我会抓住的错误 !ModelState.IsValid .

    Firstname and lastname always null

    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... 每一个要更新的属性。

    1 回复  |  直到 4 年前
        1
  •  1
  •   Máté Gábor    4 年前

    如果对输入框使用asp for属性,则不应同时使用name属性(或至少对其使用相同的值)。

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