Reputation: 1090
When I make my httppost call, i need to get the selected value and the id of a random number of dropdownlists in my view. The number of dropdownlists is random because they are created dynamically based on the selected value of another dropdownlist. Example: in dropdownlist 1 I select bmw. Then 3 dropdownlists are created, so the user can give each carmodel a rate value. Each dropdownlist has id= the model of the car and the options you can select is 1,2 and 3. If there was 4 carmodels of bmw, the options would then be 1,2,3 and 4 and so on... When I make the httppost call, how do I in the controller run through all the dynammically created dropdownlist and check the value and id?
EDIT: The controller of my index view:
public class BookingPensionController : Controller
{
private DbEntities db = new DbEntities();
EditBookingPensionViewModel objViewModel;
public ActionResult Index()
{
objViewModel = new EditBookingPensionViewModel
{
Booking = new Booking { BookingStartDate = DateTime.Today, BookingEndDate = DateTime.Today, DateOfBooking = DateTime.Now }
};
objViewModel.deliveryTypes = new List<string>();
int id = int.Parse(Session["id"].ToString());
SelectList list;
try
{
var deliverytypes = from d in db.DeliveryTypes
where d.Pension_ID == id
select d;
foreach (DeliveryType item in deliverytypes)
{
objViewModel.deliveryTypes.Add(item.Titel);
}
ViewData["DeliveryTypes"] = objViewModel.deliveryTypes;
objViewModel.customersToPension = new List<SelectListItem>();
objViewModel.customersToPension = GetCustomersToPension(id);
}
catch (Exception ex)
{
throw;
}
int PensionId = int.Parse(Session["id"].ToString());
objViewModel.CustomerValue = GetCustomersToPension(PensionId);
return View(objViewModel);
}
My Index view:
@model HundePensionBooking.ViewModels.EditBookingPensionViewModel
//Some code...
//And my script that retreives the customerinfo and creates the dropdownlists
$("#ddlCustomerId").change(function () {
var id = $("#ddlCustomerId").val();
getCustomerInfo(id);
$('#customerinfo').load("@Url.Action("CustomerInfo", "BookingPension")");
var ddlsource = "#ddlCustomerId";
var ddltarget = "#ddlDogId";
$.getJSON('@Url.Action("Dogs_SelectedCustomer")', { CustomerId: $(ddlsource).val() }, function (data) {
$(ddltarget).empty();
$("#tblRooms tr:gt(0)").remove();
$.each(data, function (index, optionData) {
createDynamicTable($("#tblRooms"), optionData.Text, data.length);
});
});
});
});
</script>
<script type="text/javascript">
function createDynamicTable(tbody, value, count) {
if (tbody == null || tbody.length < 1) return;
var trow = $("<tr>");
var cellText = value
var option;
$('<th scope="row" abbr="Model" class="spec">')
.addClass("tableCell")
.text(cellText)
.appendTo(trow);
var ddlselectroom = '<td class="spectd"><select class="selectbox" id=' + value + '>';
for (var d = 1; d <= count; d++) {
ddlselectroom += '<option value="' + d + '">Bur nr. ' + d + '</option>';
}
ddlselectroom += '</select></td>';
$(ddlselectroom).appendTo(trow);
trow.appendTo(tbody);
}
//Some more code...
//And then the table which gets populated with the random number of dropdownlists:
<table id="tblRooms" width="100%">
<tr>
</tr>
</table>
The Viewmodel looks like this:
public class EditBookingPensionViewModel
{
public Booking Booking { get; set; }
public IEnumerable<Customer> customerList { get; set; }
public List<string> deliveryTypes { get; set; }
public List<SelectListItem> customersToPension { get; set; }
public Customer customer { get; set; }
public CustomerInfoModel CustomerInfo { get; set; }
My partialview with the customerinfo looks like this:
@model HundePensionBooking.Models.Customer
So when I make the httppost call
<input type="submit" value="Create" />
I need to get all the data down to my database. Im not sure on how to do that but I assume I have to do it with my Create method in the controller class
[HttpPost]
public ActionResult Create(EditBookingPensionViewModel model)
{
//SaveCustomer... From the CustomerInfo partialview
//SaveBooking...From Index view
//Save all the data from all the dropdownlists... from index view
}
Upvotes: 0
Views: 3039
Reputation: 3932
just add a hidden field of counter, when you submit, get value of counter then loop from 1 to value of counter and request optionname + counter. jquery should be used to set the value of counter.
Upvotes: 1