gpt4 book ai didi

asp.net - 值不能为空。参数名称 : items (DrodownList)

转载 作者:行者123 更新时间:2023-12-04 18:43:52 26 4
gpt4 key购买 nike

我正在使用 ASP.NET MVC4,现在我想添加一个下拉列表,其中包含来自我的 mysql 数据库的数据。这就是我所做的:

在我的 查看 (注册.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 (帐户 Controller ):
[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;
}

我的注册模型:
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 但键 12 是 Districts 并给出错误: "The district field is required" ...

我究竟做错了什么?

最佳答案

考虑模型验证失败的情况。 View 将与请求一起发送的模型再次重新显示。然而这一行:

new SelectList(ViewBag.Districts, "district_id", "district_name", Model.Districts)

将有 null作为第一个参数,因为 ViewBag.Districts没有重新填充,导致异常。因此,为了避免异常,只需再次设置此属性:
// If we got this far, something failed, redisplay form
var districts = repository.GetDistricts();
ViewBag.Districts = districts;
return View(model);

更新。 看到模型定义,立刻想到的是 Required Districts 的属性收藏。很可能您不需要用户输入任何这些内容,也不需要将它们保存到数据库中。尝试删除此属性,有关此属性的错误将消失。

关于asp.net - 值不能为空。参数名称 : items (DrodownList),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18357321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com