Umbraco: Create CheckBoxList property with prevalues from mvc model

后端 未结 2 1161
小鲜肉
小鲜肉 2021-01-28 03:21

What I want to do is create a CheckBoxList property so the editor could choose facilities specific for current page (hotel name) in BO, and render content based on what is check

2条回答
  •  旧巷少年郎
    2021-01-28 03:35

    Your Facility model should contain a boolean value to indicate if its been selected

    public class FacilityVM
    {
      public int Id { get; set; }
      public string Description { get; set; }
      public bool IsSelected { get; set; }
    {
    
    public class HotelVM
    {
      public int ID{ get; set; }
      ....
      public List Facilities { get; set; }
    }
    

    Controller

    public ActionResult Edit(int ID)
    {
      HotelVM model = new HotelVM();
      model.Facilities = // populate the list of available facilities
      // Get the hotel from repository and map properties to the view model
      return View(model);
    }
    
    public ActionResult Edit(HotelVM model)
    {
      ...
      foreach(FacilityVM facility in model.Facilities)
      {
        if (facility.IsSelected)
        {
          // do something
        }
      }
      ....
    }
    

    View

    @model HotelVM
    @using (Html.BeginForm())
    {
      // render properties of hotel
      ....
      for (int i = 0; i < Model.Facilities.Count; i++)
      {
        @Html.HiddenFor(m => m.Facilities[i].ID);
        @Html.HiddenFor(m => m.Facilities[i].Description);
        @Html.CheckBoxFor(m => m.Facilities[i].IsSelected)
        @Html.LabelFor(m => m.Facilities[i].IsSelected, Model.Facilities[i].Description)
      }
      
    }
    

提交回复
热议问题