selected.jquery mvc5 multiselect

时间:2017-10-06 08:29:53

标签: jquery asp.net-mvc

我已将selected.jquery实现为多选下拉列表。

它正在显示创建获取,但由于某种原因没有传递到帖子。

模型

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,因为它没有将结果赋给模型属性。我不知道如何做到这一点。提前致谢

1 个答案:

答案 0 :(得分:0)

愚蠢的错误。我需要@Html.ListBoxFor(model => model.SelectedAmenities)而不是@Html.Listbox

现在正在使用