簡體   English   中英

值不能為空。 ASP.NET MVC下拉列表發布后失敗

[英]Value cannot be null. ASP.NET MVC Dropdown List Fails on Post

我正在使用ASP.NET的“注冊”視圖,並添加了一個下拉列表來保存類別,如下所示: 修改后的注冊表

這是我認為的代碼:

<div class="form-group">
    @Html.LabelFor(m => m.AccountCategory, "Account Type", new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.DropDownListFor(m => m.AccountCategory, new SelectList(Model.AccountCategories, "Id", "Category"), "", new { @class = "form-control" })
    </div>
</div>

我綁定到類別的ViewModel是:

[Required]
    public int AccountCategory { get; set; }

    public IEnumerable<AccountCategory> AccountCategories { get; set; }

我修改了Register控制器以適應category字段,如下所示:

// POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        var category = _context.Categories.Single(c => c.Id == model.AccountCategory);

        if (ModelState.IsValid)
        {
            var user = new ApplicationUser
            {
                CategoryId = category.Id,
                UserName = model.Email,
                Email = model.Email,
                FirstName = model.FirstName,
                OtherName = model.OtherName,
                LastName = model.LastName
            };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                // Send an email with this link
                // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

但是,當我單擊提交按鈕時,出現此錯誤:值不能為null。 參數名稱:項目

說明:執行當前Web請求期間發生未處理的異常。 請查看堆棧跟蹤,以獲取有關錯誤及其在代碼中起源的更多信息。

異常詳細信息:System.ArgumentNullException:值不能為null。 參數名稱:項目

源錯誤:

Line 15:     @Html.LabelFor(m => m.AccountCategory, "Account Type", new { @class = "col-md-2 control-label" })
Line 16:     <div class="col-md-10">
Line 17:         @Html.DropDownListFor(m => m.AccountCategory, new SelectList(Model.AccountCategories, "Id", "Category"), "", new { @class = "form-control" })
Line 18:     </div>
Line 19: </div>

任何協助將不勝感激。 謝謝。

您不能綁定int屬性並將其直接作為SelectList傳遞。 AccountCategories模型應如下所示:

public IEnumerable<SelectListItem> AccountCategories { get; set; }

這是將下拉列表項正確傳遞給DropDownListFor

@Html.DropDownListFor(m => m.AccountCategory, Model.AccountCategories, "", new { @class = "form-control" })

注意: IEnumerable<SelectListItem>實例應已填充IdCategory屬性值,例如new SelectListItem() { Text = Category, Value = Id }

 @Html.DropDownListFor(m => m.AccountCategory, Model.AccountCategories, "", new { @class = "form-control" })

@Html.DropDownListFor(m => m.AccountCategory,Model.AccountCategories.Select(x => new SelectListItem() { Value = x.Id, Text = x.Category}) , "", new { @class = "form-control" })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM