加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 值不能为null.参数名称:items(DrodownList)

发布时间:2020-12-16 07:05:54 所属栏目:asp.Net 来源:网络整理
导读:我正在使用ASP.NET MVC4,现在我想添加一个包含来自 mysql数据库的数据的下拉列表.这就是我做的: 在我看来(Register.cshtml):` div class="control-group" @Html.LabelFor(m = m.DistrictID,new { @class= "control-label"}) div class="controls" @Html.Dr
我正在使用ASP.NET MVC4,现在我想添加一个包含来自 mysql数据库的数据的下拉列表.这就是我做的:

在我看来(Register.cshtml):`

<div class="control-group">
    @Html.LabelFor(m => m.DistrictID,new { @class= "control-label"})
    <div class="controls">
        @Html.DropDownListFor(model => model.DistrictID,new SelectList(ViewBag.Districts,"district_id","district_name",Model.DistrictID))
    </div>
</div>

在我的Controller(AccountController)中:

[AllowAnonymous]
public ActionResult Register()
{
    var districts = repository.GetDistricts();
    ViewBag.Districts = districts;

    return View(new RegisterModel());
}

//
// POST: /Account/Register

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{

    if (ModelState.IsValid)
    {
        // Attempt to register the User
        try
        {
            MembershipService.CreateUser(model.Name,model.FamilyName,model.BirthDate,model.Sex,model.Nationality,model.Email,model.UserName,model.Password,model.Street,model.StreetNr);

            FormsAuthentication.SetAuthCookie(model.UserName,false);
            return RedirectToAction("Index","Home");
        }
        catch (ArgumentException ae)
        {
            ModelState.AddModelError("",ae.Message);
        }
    }

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

我从我的存储库中获取区域,如下所示:

public IQueryable<district> GetDistricts()
 {
     return from district in entities.districts
            orderby district.district_name
            select district;
 }

我的RegisterModel:

public class RegisterModel
{
    [Required]
    [Display(Name = "Given name")]
    public string Name { get; set; }

    [Required]
    [Display(Name = "Family name")]
    public string FamilyName { get; set; }

    [Required]
    [Display(Name = "Birthdate")]
    public DateTime BirthDate { get; set; }

    [Required]
    [Display(Name = "Sex")]
    public string Sex { get; set; }

    [Required]
    [Display(Name = "Nationality")]
    public string Nationality { get; set; }

    [Required]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [StringLength(100,ErrorMessage = "The {0} must be at least {2} characters long.",MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password",ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    [Required]
    [Display(Name = "Street")]
    public string Street { get; set; }

    [Required]
    [Display(Name = "Street Number")]
    public int StreetNr { get; set; }

    [Required]
    [Display(Name = "District")]
    public IEnumerable<SelectListItem> Districts { get; set; }

    public int DistrictID { get; set; }
}

下拉列表中有区域,但是当我点击“注册”时,我收到此错误:

Value cannot be null.
Parameter name: items

Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: items

当我调试方法时,我看到ModelState无效.
关键11是DistrictID,包括districtid但Key 12是Districts并给出错误:“区域字段是必需的”……

我究竟做错了什么?

解决方法

考虑模型验证失败的情况.对于随请求发送的模型,将再次重新显示视图.不过这一行:

new SelectList(ViewBag.Districts,Model.Districts)

将null作为第一个参数,因为ViewBag.Districts没有重新填充,导致异常.因此,为了避免异常,只需再次设置此属性:

// If we got this far,redisplay form
var districts = repository.GetDistricts();
ViewBag.Districts = districts;
return View(model);

更新.当看到模型定义时,立即进入脑海的事物是区域集合的必需属性.很可能您不需要用户输入任何这些,也不需要将它们保存到数据库中.尝试删除此属性,有关此属性的错误将消失.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读