值不能为空。发布

时间:2017-09-07 08:51:37

标签: c# asp.net-mvc

我正在使用ASP.NET的注册视图,并添加了一个下拉列表来保存类别,如下所示: Modified Registration Form

这是我的观点代码:

<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控制器以适应类别字段:

// 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);
    }

但是当我点击提交按钮时,我收到此错误: 值不能为空。 参数名称:items

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

  

异常详细信息:System.ArgumentNullException:值不能为null。   参数名称:items

来源错误:

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>

任何帮助都将不胜感激。 感谢。

2 个答案:

答案 0 :(得分:0)

您无法绑定int属性并将其直接传递为SelectListAccountCategories模型应如下所示:

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

这就是如何将下拉列表项正确传递给DropDownListFor

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

注意:IEnumerable<SelectListItem>实例应填充Id&amp; Category属性值,例如new SelectListItem() { Text = Category, Value = Id }

答案 1 :(得分:0)

改变
 @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" })