我正在构建一个包含多个标签的网络应用,其中包含与客户有关的数据。在地址选项卡上,它显示分隔客户端不同地址的其他选项卡,如计费,物理和邮寄地址。尝试编辑信息时,地址类型下拉列表不会默认为为每个地址加载的当前选定类型。我已经尝试了多种方法来实现这一点,但目前我收到以下错误:
System.Web.HttpException:DataBinding:'System.Web.Mvc.SelectListItem'不包含名为'Id'的属性
视图模型:
// GET: Clients/Edit/5
public async Task<ActionResult> Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Client client = new Client();
client = await db.Clients.FindAsync(id);
if (client == null)
{
return HttpNotFound();
}
ClientVM clientvm = new ClientVM();
clientvm.Id = client.Id;
clientvm.client_Name = client.client_Name;
clientvm.is_active = client.is_active;
clientvm.Addresses = client.Addresses;
clientvm.Depts = client.Depts;
clientvm.Contracts = client.Contracts;
clientvm.Addr_Types = new SelectList(db.Address_Type, "Id", "addr_Type");
clientvm.States = new SelectList(db.States, "Id", "name");
return View(clientvm);
}
控制器:
@Html.DropDownListFor(model => Model.Addresses[y].addr_Type_Id, new SelectList(Model.Addr_Types, "Id", "addr_Type", Model.Addresses[y].addr_Type_Id), htmlAttributes: new { @class = "form-control", id = "ddlAddr-" + @y })
Edit.cshtml:
int(75.85185185185185) # -> 75
答案 0 :(得分:0)
谢谢Stephen Muecke!那很有效。我实际上让它传递了selectList并且只使用了Addr_Types,但是无法使selectedValue工作,我唯一可以接近的方法是再次使用新的SelectList。感谢您指出再次投射它,然后改为“价值”&amp; “文本”。