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方法。但是当我添加状态下拉列表...
无效的操作异常,没有可枚举类型的视图数据