C# 具有键';CountryId';类型为';System.Int32';但必须为'型;IEnumerable<;选择列表项>';
在这个话题上有很多问题被问到,我仔细研究了很多问题,但不幸的是,我没有找到任何解决问题的方法。我想从数据库中自动填充国家、州、城市的下拉列表。下面是我设计的模型 型号:C# 具有键';CountryId';类型为';System.Int32';但必须为'型;IEnumerable<;选择列表项>';,c#,sql-server,asp.net-mvc,entity-framework,ienumerable,C#,Sql Server,Asp.net Mvc,Entity Framework,Ienumerable,在这个话题上有很多问题被问到,我仔细研究了很多问题,但不幸的是,我没有找到任何解决问题的方法。我想从数据库中自动填充国家、州、城市的下拉列表。下面是我设计的模型 型号: public class Registration { [Required(ErrorMessage="Please Select a Country")] public int CountryId { get; set; } public IList<Sel
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; }
}
这是我的注册视图
@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>
}
@model I\u am\u a\u freasher.Models.UserModel.Registration
@使用(Html.BeginForm(“,”,FormMethod.Post,new{@id=“formstep1”,@class=“active”}))
{
@Html.DropDownListFor(model=>model.CountryId,model.AvailableCountries)
@Html.ValidationMessageFor(m=>m.CountryId,null,new{@class=“text danger error bold”})
@DropDownListFor(model=>model.StateId,model.availableEstates)
@ValidationMessageFor(m=>m.StateId,null,新的{@class=“text danger error bold”})
请稍等。。
}
我不清楚为什么会出现这个错误。我认为这应该行得通。有人能告诉我为什么会出现此错误吗?您的问题是,您正在
try
块内的可用国家/地区中添加SelectListItems
,但属性尚未初始化,因此引发异常。然后返回视图,但AvailableCountries
仍为null
,这将导致您看到的错误消息
或者在模型中的无参数构造函数中初始化它
public class Registration
{
public Registration()
{
AvailableCountries = new List<SelectListItem>();
}
....
}
与控制器连接(2行代码与10行代码)
在我看来
@Html.DropDownListFor(m => m.CountryId, Model.AvailableCountries, "Please Select your Country")
这将第一个选项呈现为“请选择您的国家”,但没有更合适的值
属性
注意:您还需要为AvailableStates
和availablespecities
初始化一个空的SelectList
,假设它们是使用javascript/jquery填充的
userreg.AvailableStates = new SelectList(Enumerable.Empty<SelectListItem>());
userreg.AvailableStates=newselectlist(Enumerable.Empty());
“我认为这应该有效。”经典。你是在初次加载时还是在发回时出现此错误?@StephenMuecke。。在初始加载本身时..调试代码。您没有设置userreg.AvailableCountries=new List
,因此您得到的异常仍然返回视图,但是AvailableCountries
是null@HadiHassan那跟它有什么关系。这条消息是因为AvailableCountries
为null
,如果您需要jquery的帮助,这会更有帮助。我需要一点帮助。现在如何通过jquery禁用第一个选项?这里说我想禁用“请选择你的国家”。这是选择中的第一个选项,所以(假设你想在用户第一次选择其他选项时禁用它)$('#CountryId').change(function(){$(this.children('option').first().prop('disabled',true);})
(或者您可以执行.first().remove();
来删除它,但您需要以$('form.)的形式执行此操作。一个('change','#CountryId',function(){..
,因此它只执行一次
public SelectList AvailableCountries { get; set; }
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")
userreg.AvailableStates = new SelectList(Enumerable.Empty<SelectListItem>());