ASP.NET Core MVC-无效的操作异常没有Ienumerable ]类型的视图数据< 我正在尝试根据在国家/地区下拉列表中选择的国家/地区名称来级联国家名称。我已经为所选国家/地区编写了JS for post方法。但是,当我在视图中添加状态下拉列表并对其进行调试时。我收到错误-没有键StateName1的Ienumerable类型的Viewdata。 NewRequestView模型 public class NewRequestView { public string SelectedCountry { get; set; } public IEnumerable<SelectListItem> Countries { get; set; } public IEnumerable<SelectListItem> State { get; set; } } 国家型号 public class Country { public int CountryID { get; set; } public string CountryName { get; set; } } 控制器 public IActionResult NewRequest() { var model = new NewRequestView { }; model.Countries = iMapper.Map<IEnumerable<Country>, IEnumerable<SelectListItem>>(GetCountries()); return View(model); } 查看 <div class="row"> <div class="col-xs-10 multiSelectCheckboxDiv" id="multiSelectCheckboxDiv"> <span class="col1 contrlLabel" style=" font-family:calibri;color:gray;font-size:15px">Clients<span style="font-weight:800;color:red;">*</span>:</span> @Html.DropDownList("CountryName", Model.Countries ,"Select Country",new { @class = "form-control"}) <div class="input-validation-errorText col-md-6" style="display:none;"> Please select Country. </div> </div> </div> <div class="row"> <div class="col-xs-10 multiSelectCheckboxDiv" id="multiSelectCheckboxDiv"> <span class="col1 contrlLabel" style=" font-family:calibri;color:gray;font-size:15px">Clients<span style="font-weight:800;color:red;">*</span>:</span> @Html.DropDownList("StateName1", Model.State, "Select State", new { @class = "form-control" }) <div class="input-validation-errorText col-md-6" style="display:none;"> Please select state. </div> </div> </div> 我使用AutoMapper映射国家/地区名称。 映射器 var config = new MapperConfiguration(cfg => { cfg.CreateMap<Client, SelectListItem>() .ForMember(x => x.Text, opt => opt.MapFrom(o => o.CountryName)) .ForMember(x => x.Value, opt => opt.MapFrom(o => o.CountryName)); }); return config.CreateMapper(); [从选定国家/地区出发,我解雇了JS以获取所有州名。但是在我选择自己之前,我得到了错误 InvalidOperationException:没有类型为“ IEnumerable”的ViewData项目具有键“ StateName1”。 让我知道我在这里做什么错误 我正在尝试根据在国家/地区下拉列表中选择的国家/地区名称来级联国家名称。我已经为所选国家/地区编写了JS for post方法。但是当我添加状态下拉列表... 无效的操作异常,没有可枚举类型的视图数据 这是由于视图中提供给Html.DropdownList()的空列表所致。 看您的控制器动作,您没有能提供model.State值的任何东西。 public IActionResult NewRequest() { var model = new NewRequestView { }; model.Countries = iMapper.Map<IEnumerable<Country>, IEnumerable<SelectListItem>>(GetCountries()); // you need something like this model.State = new List<SelectListItem>(){ new SelectListItem(){ Text = "Test", Value = "Test" } }; return View(model); } [另一种选择是,由于一旦选择了一个国家,州记录就会来自ajax事件,因此您实际上可以将模型保留为空白。州为空白,不使用Html.Dropdown()。 您可以执行此操作;提交表单时,手动创建的选择字段仍将被视为模型的一部分。 <div class="row"> <div class="col-xs-10 multiSelectCheckboxDiv" id="multiSelectCheckboxDiv"> <span class="col1 contrlLabel" style=" font-family:calibri;color:gray;font-size:15px">Clients<span style="font-weight:800;color:red;">*</span>:</span> <select id="StateName1" name="Model.State" class="form-control"> <option>Select State</option> </select> <div class="input-validation-errorText col-md-6" style="display:none;"> Please select state. </div> </div> </div> 您需要通过将Country ID传递给您的方法并通过JSON返回StateList来进行AJAX调用以获取状态。 检查Cascaded Dropdown Example的逐步说明。

问题描述 投票:0回答:2
我正在尝试根据在国家/地区下拉列表中选择的国家/地区名称来级联国家名称。我已经为所选国家/地区编写了JS for post方法。但是,当我在视图中添加状态下拉列表并对其进行调试时。我收到错误-没有键StateName1的Ienumerable类型的Viewdata。

NewRequestView模型

public class NewRequestView { public string SelectedCountry { get; set; } public IEnumerable<SelectListItem> Countries { get; set; } public IEnumerable<SelectListItem> State { get; set; } }

国家型号

public class Country { public int CountryID { get; set; } public string CountryName { get; set; } }

控制器

public IActionResult NewRequest() { var model = new NewRequestView { }; model.Countries = iMapper.Map<IEnumerable<Country>, IEnumerable<SelectListItem>>(GetCountries()); return View(model); }

查看

<div class="row"> <div class="col-xs-10 multiSelectCheckboxDiv" id="multiSelectCheckboxDiv"> <span class="col1 contrlLabel" style=" font-family:calibri;color:gray;font-size:15px">Clients<span style="font-weight:800;color:red;">*</span>:</span> @Html.DropDownList("CountryName", Model.Countries ,"Select Country",new { @class = "form-control"}) <div class="input-validation-errorText col-md-6" style="display:none;"> Please select Country. </div> </div> </div> <div class="row"> <div class="col-xs-10 multiSelectCheckboxDiv" id="multiSelectCheckboxDiv"> <span class="col1 contrlLabel" style=" font-family:calibri;color:gray;font-size:15px">Clients<span style="font-weight:800;color:red;">*</span>:</span> @Html.DropDownList("StateName1", Model.State, "Select State", new { @class = "form-control" }) <div class="input-validation-errorText col-md-6" style="display:none;"> Please select state. </div> </div> </div>

我使用AutoMapper映射国家/地区名称。

映射器

var config = new MapperConfiguration(cfg => { cfg.CreateMap<Client, SelectListItem>() .ForMember(x => x.Text, opt => opt.MapFrom(o => o.CountryName)) .ForMember(x => x.Value, opt => opt.MapFrom(o => o.CountryName)); }); return config.CreateMapper();

[从选定国家/地区出发,我解雇了JS以获取所有州名。但是在我选择自己之前,我得到了错误

InvalidOperationException:没有类型为“ IEnumerable”的ViewData项目具有键“ StateName1”。

让我知道我在这里做什么错误

我正在尝试根据在国家/地区下拉列表中选择的国家/地区名称来级联国家名称。我已经为所选国家/地区编写了JS for post方法。但是当我添加状态下拉列表...

asp.net-mvc asp.net-core ienumerable cascadingdropdown viewdata
2个回答
0
投票
无效的操作异常,没有可枚举类型的视图数据

0
投票
您需要通过将Country ID传递给您的方法并通过JSON返回StateList来进行AJAX调用以获取状态。
© www.soinside.com 2019 - 2024. All rights reserved.