C# 在Dropdownlist mvc4中编辑和更新
我试图在我的应用程序中实现dropdownlist中的编辑和更新。dropdownlist的值列表将从模型中显示。但所选值不会显示在dropdownlist上。选定的值也将填充为dropdownlist中的值列表 我的模型:C# 在Dropdownlist mvc4中编辑和更新,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我试图在我的应用程序中实现dropdownlist中的编辑和更新。dropdownlist的值列表将从模型中显示。但所选值不会显示在dropdownlist上。选定的值也将填充为dropdownlist中的值列表 我的模型: public string State public SelectList RegionList { get; set; } public class Region { public string ID { get; set; } public string Name
public string State
public SelectList RegionList { get; set; }
public class Region
{
public string ID { get; set; }
public string Name { get; set; }
}
看法
注:项目状态已填充,但未显示值
填充并显示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);
}
假设
AddressList
属性是一个IList
尝试如下操作:
@for (var i = 0; i < Model.AddressList.Count; i++)
{
@Html.DropDownListFor(
model => model.AddressList[i].State,
new SelectList(Model.Address.RegionList, "Value", "Text")
)
}
(变量i=0;iforeach
替换为for
:
@for (int i=0; i<Model.AddressList.Count; i++)
{
@Html.DropDownListFor(model => model.AddressList[i].State, new SelectList(Model.Address.RegionList, "Value", "Text", Model.Address.RegionList.SelectedValue))
}
@for(int i=0;i model.AddressList[i]。状态,新选择列表(model.Address.RegionList,“Value”,“Text”,model.Address.RegionList.SelectedValue))
}
model.AddressList[i]。状态值未填充为列表中的值之一,也未显示为所选值。填充了item.State值,但未填充model.AddressList[i]。状态populated@kk1076,这听起来有点奇怪,因为它们基本上是一样的。
@for (var i = 0; i < Model.AddressList.Count; i++)
{
@Html.DropDownListFor(
model => model.AddressList[i].State,
new SelectList(Model.Address.RegionList, "Value", "Text")
)
}
model => item.State
@for (int i=0; i<Model.AddressList.Count; i++)
{
@Html.DropDownListFor(model => model.AddressList[i].State, new SelectList(Model.Address.RegionList, "Value", "Text", Model.Address.RegionList.SelectedValue))
}