如何填写dropdownlistfor post

时间:2017-09-19 11:16:53

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

我有

<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>

发帖后我采取modalstate错误,然后我错过了区。如何在错误发布后设置分区

1 个答案:

答案 0 :(得分:0)

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

 return View(model)
然后我以这种方式重新填写列表。

@{ 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, _district, new { @class = "dropdownList"});

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