1

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

1 に答える 1

0

ListBoxモデルに複数のテナントがあるため、 (複数選択が可能な) を探している可能性があります。

@Html.ListBoxFor(model => model.TenantID, ((IEnumerable<FlatSystem.Models.Tenants>)ViewBag.TenantLookupList).Select(option => new SelectListItem
    {
        Text = (option == null) ? "None" : option.Firstname),
        Value = option.TenantID.ToString()
    }), "Choose...")

Selectedモデルによって決定されるため、プロパティを明示的に設定する必要はありません。Model.TenantIDコレクション内のすべての項目が選択されているはずです。


選択した項目を設定する場合は、 を使用する必要がありますHtml.ListBox

@Html.ListBox("MyListBox", ((IEnumerable<FlatSystem.Models.Tenants>)ViewBag.TenantLookupList).Select(option => new SelectListItem
{
    Text = (option == null) ? "None" : option.Firstname),
    Value = option.TenantID.ToString(),
    Selected = Model.TenantID.Any(tenant => tenant.ID == option.TenantID)
}), "Choose...")
于 2012-09-27T05:55:52.213 に答える