我試圖在我的應用程序的dropdownlist中實現編輯和更新。從模型中顯示下拉列表的值列表。但是選定的值不會顯示在下拉列表中。所選值也作爲下拉列表中的值列表填充。在Dropdownlist中編輯和更新mvc4
我的模型:
public string State
public SelectList RegionList { get; set; }
public class Region
{
public string ID { get; set; }
public string Name { get; set; }
}
查看
@foreach (var item in Model.AddressList)
{
@Html.DropDownListFor(model => item.State, new SelectList(Model.Address.RegionList, "Value", "Text", Model.Address.RegionList.SelectedValue))
}
注:
item.State被填充,但不顯示值
model.address.regionlist填充並顯示
控制器
public ActionResult EditAddress(AddressTuple tmodel,int AddressID)
{
int country;
string customerID = 1;
List<AddressModel> amodel = new List<AddressModel>();
amodel = GetAddressInfo(customerID, AddressID); // returns the selected value for dropdown
foreach (var item in amodel)
{
country = item.CountryId;
}
List<Region> objRegion = new List<Region>();
objRegion = GetRegionList(id); // returns the list of values for dropdown
SelectList objlistofregiontobind = new SelectList(objRegion, "ID", "Name", 0);
atmodel.RegionList = objlistofregiontobind;
tmodel.Address = atmodel;
tmodel.AddressList = amodel;
return View(tmodel);
}
對於在下拉列表中進行編輯,將顯示值列表。但不顯示選定的值。什麼是我的代碼中的錯誤。任何建議。
編輯:
@Html.DropDownListFor(model => model.State, new SelectList(Model.RegionList, "ID", "Name",Model.State))
model.AddressList [I] .STATE值不填充在列表中的值之一,它不顯示爲選擇的值。 – kk1076