具有键';CountryId';属于';System.Int32';但必须是';IEnum

本文关键字:IEnum Int32 属于 CountryId System | 更新日期: 2023-09-27 18:27:57

关于这个主题,有很多问题被问到了,我已经回答了很多,但不幸的是,我没有找到任何解决问题的方法。我想从DB中自动填充国家城市的下拉列表。下面是我设计的模型。

型号:

    public class Registration
    {
        [Required(ErrorMessage="Please Select a Country")]
        public int CountryId { get; set; }
        public IList<SelectListItem> AvailableCountries { get; set; }
        [Required(ErrorMessage = "Please Select a State")]
        public int StateId { get; set; }
        public IList<SelectListItem> AvailableStates { get; set; }
        [Required(ErrorMessage = "Please Select a City")]
        public int CityId { get; set; }
        public IList<SelectListItem> AvailableCities { get; set; }
    }

当一个新用户登录时,我将他重定向到用户控制器的注册页面。

以下是我的用户控制器的GetActionResult

 public ActionResult Registration()
    {
        UserModel.Registration userreg = new UserModel.Registration();
        try
        {
            userreg.AvailableCountries.Add(new SelectListItem { Text = "Please Select your Country", Value = "Countries" });
            var query = from countries in user.Countries orderby countries select countries;
            var content = query.ToList();
            foreach (var country in content)
            {
                userreg.AvailableCountries.Add(new SelectListItem
                {
                    Text = country.Name,
                    Value = country.Id.ToString()
                });
            }
            return View(userreg);
        }
        catch(Exception ex)
        {
            Helpers.Functions_ErrorLog.LogMessage("Error from User/Registration - " + ex.Message.ToString());
        }
        return View(userreg);
    }

这是我的注册视图

    @model I_am_a_Fresher.Models.UserModel.Registration
    @using (Html.BeginForm("", "", FormMethod.Post, new { @id = "formstep1", @class = "active" }))
        {
            <div id="stepsbody" class="col-md-12 col-xs-12 col-lg-12 col-sm-12 mar-top-22">
  <div class="form-group col-md-6 col-lg-6 col-sm-12 col-xs-12">
                        <span class="ico-state-city"></span>
                        @Html.DropDownListFor(model => model.CountryId, Model.AvailableCountries)
                        @Html.ValidationMessageFor(m => m.CountryId, null, new { @class = "text-danger error-bold" })
                    </div>
                    <div class="form-group col-md-6 col-lg-6 col-sm-12 col-xs-12">
                        <span class="ico-state-city"></span>
                        @Html.DropDownListFor(model => model.StateId, Model.AvailableStates)
                        @Html.ValidationMessageFor(m => m.StateId, null, new { @class = "text-danger error-bold" })
                        <span id="states-loading-progress" style="display: none;">Please wait..</span>
                    </div>
                </div>
    }

我不清楚为什么会出现这个错误。根据我的说法,这应该是可行的。有人能说明为什么会出现这个错误吗?

具有键';CountryId';属于';System.Int32';但必须是';IEnum

您的问题是在try块中将SelectListItems添加到AvailableCountries,但属性尚未初始化,因此引发异常。然后返回视图,但AvailableCountries仍然是null,这将导致您看到的错误消息。

要么在模型中的无参数构造函数中初始化它

public class Registration
{
  public Registration()
  {
    AvailableCountries = new List<SelectListItem>();
  }
  ....
}

或在控制器方法中

UserModel.Registration userreg = new UserModel.Registration();
userreg.AvailableCountries = new List<SelectListItem>();
try
  .....

然而,我建议您将模型属性更改为

public SelectList AvailableCountries { get; set; }

控制器到(2行代码对10)

var query = (from countries in user.Countries orderby countries select countries).ToList();
userreg.AvailableCountries = new SelectList(query, "Id", "Name");

并且在视图中

@Html.DropDownListFor(m => m.CountryId, Model.AvailableCountries, "Please Select your Country")

它将第一个选项呈现为"请选择您的国家",但没有value属性,这更适合

还请注意,您需要为AvailableStatesAvailableCities初始化一个空的SelectList,假设它们是使用javascript/jquery 填充的

userreg.AvailableStates = new SelectList(Enumerable.Empty<SelectListItem>());
相关文章: