【问题标题】:how to fill dropdownlistfor after post发布后如何填写下拉列表
【发布时间】:2018-02-28 03:47:09
【问题描述】:

我有

<form>
    @Html.DropDownListFor(x => x.City, provinces, "--Select City--", new { @class = "dropdownList" })
    @Html.DropDownListFor(x => x.district, Enumerable.Empty<SelectListItem>(), "--Select district--")
    <input type="submit" id="submitRegister" value="submit" />
</form>

发布后我出现模态状态错误,然后我错过了地区。错误发布后如何设置地区

【问题讨论】:

  • 这似乎是级联下拉菜单(并且您没有显示任何与此相关的代码),但我建议您参考this answer

标签: javascript jquery ajax asp.net-mvc forms


【解决方案1】:

我理解并解决了这个问题。 我更新了控制器

 返回视图(模型)
然后我以这种方式重新填写列表。

@{ if (Model != null)
var _id = Convert.ToInt32(Model.City);
var DistrictService = DependencyResolver.Current .GetService();
var district= DistrictService.Search(x => x.ProvinceId == _id).ToList();
List _district = new List();
foreach (var item in District )
{
_district.Add(new SelectListItem { Value = item.Id.ToString(), Text = item.Name});
}
@Html.DropDownListFor(x => x.区,_district,新 { @class= "dropdownList"});

else {
@Html.DropDownListFor(x => x.district, Enumerable.Empty(), "-- Select--", new { @class= "dropdownList", disabled = true });
}

【讨论】:

    猜你喜欢
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 2022-01-16
    相关资源
    最近更新 更多