试图将视图字段设置为字符串,但未提交字符串

Attempting to set view field to a string, but string is not being submitted

为了提供一些背景信息,我正在尝试为登录时需要更改的帐户设置一个临时密码。

我正在创建“随机字符串”,并将其设置为此处的 ViewBag 变量:

        public IActionResult CreateAdminAccount()
        {
            Random rng = new Random();
            var num = rng.Next(1, 100);

            ViewBag.Password = "Administrator" + num + "!";

            return View();
        }

我正在将 Password 字段的值设置为此变量以明文形式查看(已禁用,因此无法更改该字段)。

            <!-- rest of form omitted for brevity -->

            <div class="form-group">
                <label asp-for="Password" class="control-label"></label>
                <input asp-for="Password" class="form-control" value="@ViewBag.Password" disabled />
                <span asp-validation-for="Password" class="text-danger"></span>
            </div>

当我提交表单时,我收到一条验证错误消息,指出“需要密码字段”。我缺少一些互动吗?因为我能够在视图中看到这个值,所以我的想法是它将与表单以及其余字段一起提交(下面的 Pertinent ViewModel 属性)。

        [Required]
        public string Password { get; set; }

请注意,将不会提交表单中 disabled 个元素。您可以尝试将其设置为 readonly,如下所示。

<input asp-for="Password" class="form-control" value="@ViewBag.Password" readonly />