2017-10-06 12 views
0

私は複数選択ドロップダウンのためにselected.jqueryを実装しました。selected.jquery mvc5 multiselect

作成時に表示されていますが、何らかの理由で投稿にパススルーされません。

モデル

public class Room 
{ 
    [Key] 
    public int Id { get; set; } 
    [DisplayName("Room Number")] 
    public int RoomNumber { get; set; } 
    [DisplayName("Room Type")] 
    public int RoomTypeId { get; set; } 
    public virtual RoomType RoomType { get; set; } 
    [DisplayName("Price Per Night")] 
    public decimal Price { get; set; } 
    public virtual ICollection<Amenity> Amenities { get; set; } 
    [NotMapped] 
    public IEnumerable<string> SelectedAmenities { get; set; } 
    [DisplayName("Bed Type")] 
    public int BedTypeId { get; set; } 
    public virtual BedType BedType { get; set; } 
} 

コントローラ

 // GET: Rooms/Create 
    [MultiTenantControllerAllow("ManagementPortal")] 
    public ActionResult Create() 
    { 

     ViewBag.RoomTypeId = new SelectList(db.RoomTypes, "Id", "Name"); 
     ViewBag.BedTypeId = new SelectList(db.BedTypes, "Id", "Name"); 
     ViewBag.AmenityIds = new SelectList(db.Amenities, "Id", "Name"); 
     return View(); 
    } 

    // POST: Rooms/Create 
    [MultiTenantControllerAllow("ManagementPortal")] 
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Create([Bind(Include = "Id,RoomNumber,RoomTypeId,BedTypeId,Price,Amenities,SelectedAmenities")] Room room) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Rooms.Add(room); 
      await db.SaveChangesAsync(); 
      return RedirectToAction("Index"); 
     } 

     ViewBag.RoomTypeId = new SelectList(db.RoomTypes, "Id", "Name", room.RoomTypeId); 
     ViewBag.BedTypeId = new SelectList(db.BedTypes, "Id", "Name", room.BedTypeId); 
     ViewBag.AmenityIds = new SelectList(db.Amenities, "Id", "Name", room.Amenities); 
     return View(room); 
    } 

ビューセクション

<div class="form-group"> 
     @Html.LabelFor(model => model.Amenities, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.ListBox("AmenityIds", new MultiSelectList(ViewBag.AmenityIds, "Value", "text"), new { @class = "form-control amenities-select" }) 
      @Html.ValidationMessageFor(model => model.Amenities, "", new { @class = "text-danger" }) 
     </div> 
    </div><div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" value="Create" class="btn btn-default" /> 
     </div> 
    </div> 
</div> 

スクリプトセクション

@section Scripts { 
@Scripts.Render("~/bundles/jquery") 
@Scripts.Render("~/bundles/jqueryval") 
$(".amenities-select").chosen(); 
<link href="@Url.Content("~/Content/chosen.css")" rel="stylesheet" type="text/css" /> 
} 

今、問題のあるコード行は、モデルプロパティに結果を代入していないため、ListBoxだと思います。私はその仕事をどうやって作るかわからない。事前に感謝

+0

あなたはAmenityIds' –

+0

あなたが望むあなたのモデルに基づいて 'という名前のモデル内のプロパティを持っていない今働いている代わりに

@Html.ListBoxFor(model => model.SelectedAmenities)を必要と' @ Html.ListBoxFor(M => m.SelectedAmenities 、(IEnumerable )ViewBag.AmenityIds、new(...}) 'です。 'LabelFor()'と 'ValidationMessageFor()'も同じプロパティにバインドする必要があります。 –

+0

さらに、ビューで 'new SelectList(..)'を使って2番目の同じ 'IEnumerable 'を作成するのは、無意味な余分なオーバーヘッドです –

答えて

0

愚かな間違い。私はそれが