如何从mvc3中的下拉列表中获取选定的值和ID?

时间:2012-02-09 18:39:40

标签: asp.net-mvc-3

当我进行httppost呼叫时,我需要在我的视图中获取所选值和随机数下拉列表的ID。下拉列表的数量是随机的,因为它们是根据另一个下拉列表的选定值动态创建的。 例: 在下拉列表1中我选择宝马。然后创建3个下拉列表,因此用户可以为每个carmodel提供一个速率值。每个下拉列表都有id =汽车的型号,您可以选择的选项是1,2和3.如果有2个宝马汽车模型,那么选项将是1,2,3和4等等...... 当我进行httppost调用时,如何在控制器中运行所有动态创建的下拉列表并检查值和id?

编辑: 索引视图的控制器:

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);
    }

我的索引视图:

@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>

Viewmodel看起来像这样:

    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; }

我对customerinfo的部分查看如下:

@model HundePensionBooking.Models.Customer

所以当我进行httppost呼叫时

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

我需要将所有数据下载到我的数据库中。我不知道怎么做,但我想我必须在控制器类

中使用我的Create方法
[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
    }

1 个答案:

答案 0 :(得分:1)

只需添加一个隐藏的计数器字段,当你提交时,获取计数器的值然后从1循环到计数器的值并请求optionname + counter。应该使用jquery来设置counter的值。