我正在尝试将多个租户添加到 House 实体。我的错误是下拉框 (option.TenantID == Model.TenantID) 我不知道如何比较 int 和 Icollection。
模型
namespace FlatSystem.Models
{
[Table("House")]
public class House
{
[Key]
public int HouseID { get; set; }
public virtual ICollection<Tenants> TenantID { get; set; }
public House() {
TenantID = new HashSet<Tenants>();
}
}
}
控制器
public ActionResult Edit(int id)
{
ViewBag.TenantLookupList = TenantGR.All;
return View(GR.Find(id));
}
//
// POST: /House/Edit/5
[HttpPost]
public ActionResult Edit(House house)
{
if (ModelState.IsValid)
{
GR.InsertOrUpdate(house);
GR.Save();
return RedirectToAction("Index");
}
return View(house);
}
编辑视图
@using (Html.BeginForm("AddRole", "Role", new { houseId = @Model.HouseID }))
{
<table>
<tr>
<td>Select to Add Item</td>
<td>
<div class="editor-field">
@Html.DropDownListFor(model => model.TenantID, ((IEnumerable<FlatSystem.Models.Tenants>)ViewBag.TenantLookupList).Select(option => new SelectListItem
{
Text = (option == null ? "None" : option.Firstname),
Value = option.TenantID.ToString(),
Selected = (Model != null) && (option.TenantID == Model.TenantID) <<----Error
}), "Choose...")
</div>
</td>
<td><input type="submit" value="Add" /></td>
</tr>
</table>
}
错误
Operator '==' cannot be applied to operands of type 'int' and 'System.Collections.Generic.ICollection<FlatSystem.Models.Tenants>'