0

Though it seems like it has been discussed earlier also , but I could not find the solution -:

I have a VM like this-:

 public class RoomsVm
    {
        public RoomsVm()
        {


          Rooms.Add(new Room()
          {
              IsVip = true,Id = "1"

          }
          );
          Rooms.Add(new Room()
          {
              IsVip = false,
              Id = "2"
          }
          );  
        }

        public List<Room> Rooms=new List<Room>();
    }

    public class Room
    {
        public bool IsVip { get; set; }
        public string Id { get; set; }

    }

My controller has 2 simple actions- 1- for get and 2 for post-:

 public ActionResult IndexView2()
        {
            var roomVM = new RoomsVm();
            return View(roomVM);
        }
        [HttpPost]
        public ActionResult IndexView2(RoomsVm roomsVm)
        {

            return View(roomsVm);
        }

In my view - I simply display my rooms with check boxes like this-:

@model MVCApplication.Models.RoomsVm
<h2>IndexView2</h2>

@using (Html.BeginForm())
{
for (int i = 0; i < Model.Rooms.Count(); i++)
{
    @Html.CheckBoxFor(modelItem=>modelItem.Rooms[i].IsVip,new{value=Model.Rooms[i].Id})
}

<input type="submit" value="Submit"/>

}

Problem- When I click Submit button, I don't get updated values of rooms checkboxes in input value of Post action ..

What wrong am I doing ?

1 Answer 1

1

You don't have accessors on your Rooms property

public class RoomsVm
{
  public RoomsVm()
  {
    Rooms = new List<Room>(); // Initialize rooms
    Rooms.Add(new Room() {.... //Add rooms
  }
  public List<Room> Rooms { get; set; } // Add get/set
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.