0

I need to create a random number of dropdownlists in my view, based on the selected value of another dropdownlist. This is all done but my problem comes when I need to make the httppost because i never know how much data i need to save in my db.

In my model I have a list

public List<RoomToBooking> RoomsToBooking { get; set; }

that will get filled with x number of RoomToBooking when the Create view is rendered after the user makes a selction of dropdownlist 1:

var dogs = from d in db.Dogs
                       where d.Customer_ID == id
                       select d;
            foreach (Dog item in dogs)
            {
                roomToBooking = new RoomToBooking();
                roomToBooking.Customer_ID = id;
                roomToBooking.Dog = item;
                roomsToBookingList.Add(roomToBooking);
            }

So I would like to create the same number of dropdownlist in my Create view

 @Html.DropDownListFor(model => model.Booking.RoomToBooking, new SelectList(ViewBag.DeliveryTypes), new { @class = "selectbox" })
        @Html.ValidationMessageFor(model => model.Booking.RoomToBooking)

So I in the end can be able to save it to my db

[HttpPost]
    public ActionResult Create(EditBookingPensionViewModel model)
    {
        foreach (RoomToBooking item in objViewModel.RoomsToBooking)
        {
            //Save to db
        }            
    }

I assume that I should use jquery to create the dropdownlists, but how do i create the dropdownlists so the selected values can be found in my viewmodel??

1 Answer 1

1

You may take a look at the following article. I slight adaption might be necessary for your scenario because you don't have add and remove buttons but instead you use the selected value of a dropdownlist to determine the number of dynamic rows to be added. But the concept is the exactly the same.

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.