MVC 5 DropDownListFor &选择列表在发布时导致错误

本文关键字:布时导 错误 列表 DropDownListFor 选择 MVC | 更新日期: 2023-09-27 18:08:56

所以我认为这将是非常简单的,但我很难弄清楚。

首先,我将回顾一下目前为止我所知道的。

RegisterViewModel:

public class RegisterViewModel
{
    [Display(Name = "First Name:")]
    public string FirstName { get; set; }
    [Display(Name = "Last Name:")]
    public string LastName { get; set; }
    [Required]
    [EmailAddress]
    [Display(Name = "Email:")]
    public string Email { 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; }
    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Confirm password:")]
    [System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
    [Display(Name = "Please choose the category that best describes your professional relation to the field of Orthotics and Prosthetics:")]
    public List<string> ProfessionalRelations { get; set; }
    [Display(Name = "Please check all of the following that apply to you:")]
    public List<string> UserRelations { get; set; }
    [Required]
    [Display(Name = "Country")]
    public SelectList Countries { get; set; }
    [Required]
    [Display(Name = "EDGE Direct E-mail Newsletter:")]
    public List<string> EdgeNewsletter { get; set; }
}

注意国家的SelectList。

现在,在控制器中。当有人进入注册页面时,我按如下方式填充国家选择列表:

var countries = oandpService.GetCountries().OrderBy(x => x.CountryName).ToList();
var usa = countries.FirstOrDefault(x => x.CountryID == "US");
countries.Remove(usa);
countries.Insert(0, new Country() { CountryID = string.Empty, CountryName = string.Empty });
countries.Insert(1, usa);
rvm.Countries = new SelectList(countries, "CountryID", "CountryName");

最后,这是国家视图的一部分DropDownListFor:

@Html.DropDownListFor(x => x.Countries, Model.Countries, Model.Countries)

当您进入页面时,这些都显示良好。生成国家下拉列表,以CountryID作为值,CountryName作为显示文本。

然后,只是为了测试单选按钮,复选框和下拉框,我有以下内容:

[HttpPost]
[ValidateAntiForgeryToken]
[ReCaptchaFilter]
public ActionResult Register(RegisterViewModel rvm)
{
    ViewBag.Countries = rvm.Countries.SelectedValue;
    ViewBag.UserRelation = rvm.UserRelations;
    ViewBag.ProfessionalRelation = rvm.ProfessionalRelations;
    return View(rvm);
}

除了我收到以下错误的邮件:

  1. 没有为这个对象定义无参数构造函数。
  2. 没有为这个对象定义无参数构造函数。对象类型'System.Web.Mvc.SelectList'.
谁能帮我指一下正确的方向?我做了一些调查,但还没有弄清楚。

MVC 5 DropDownListFor &选择列表在发布时导致错误

您使用了错误的帮助器方法!查看页面的查看源!您当前的代码正在生成名称为"nations"的SELECT元素。提交表单时,将根据表单元素键"nations"提交所选值。当模型绑定器试图将这些提交的值映射到RegisterViewModel的实例时,它会看到SelectList类型的"国家"(根据视图模型类定义),并且它不能从整数值(从表单提交的Selected CountryId)构建SelectList对象。这就是你看到这个错误的原因!这一切的原因是,因为您错误地使用了helper方法!

理想情况下,你应该添加另一个属性来保存选择的值。

public class RegisterViewModel
{
   [Required]
   [Display(Name = "Country")]
   public int SelectedCountry { set;get;}
   public SelectList Countries { get; set; }
   //Your existing properties goes here
}

和视图

@Html.DropDownListFor(x => x.SelectedCountry, Model.Countries )

和在HttpPost操作中,您将在SelectedCountry属性

中获得所选选项的值。
public ActionResult Register(RegisterViewModel rvm)
{
    var selectedCountry = rvm.SelectedCountry;
    //to do : Reload Countries property of rvm again here(same as your GET action)
    return View(rvm);
}

同样,要添加空选择选项,您不需要在控制器代码中添加空项,您可以简单地使用DropDownListFor helper方法的重载。

@Html.DropDownListFor(x => x.SelectedCountry, Model.Countries,string.Empty)