2012-02-09 162 views
0

當我進行我的httppost調用時,我需要在我的視圖中獲取所選值和隨機數下拉列表的ID。下拉列表的數量是隨機的,因爲它們是基於另一個下拉列表的選定值動態創建的。 例如: 在下拉列表中1我選擇bmw。然後創建3個下拉列表,因此用戶可以給每個carmodel一個費率值。每個下拉列表都有id =車的型號,您可以選擇的選項爲1,2和3.如果有4輛carmodel的bmw,則選項爲1,2,3和4,等等...... 當我進行httppost調用時,如何在控制器中運行所有動態創建的下拉列表並檢查值和ID?如何從mvc3中的下拉列表中獲取選定的值和和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); 
    } 

我的索引視圖:

//Some more code... 
//And then the table which gets populated with the random number of dropdownlists: 
<table id="tblRooms" width="100%"> 
    <tr> 
</tr> 
    </table> 

視圖模型看起來是這樣的:

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 partialview看起來是這樣的:

@model HundePensionBooking.Models.Customer 

所以,當我做出httppost呼叫

<input type="submit" value="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 
    } 
+0

什麼是在FormCollection上的帖子? – 2012-02-09 21:05:33

+0

請發佈您現在的代碼,我們會更容易看到問題。 – 2012-02-10 06:49:29

+0

添加了som代碼...只是說如果你需要更多的:) – Christian 2012-02-10 21:43:35

回答

1

只需添加計數器的隱藏字段,當你提交,從中獲取計數器的值,那麼循環1到計數器的值並請求選項名稱+計數器。應該使用jquery來設置計數器的值。

+0

聽起來有趣...我應該能夠添加隱藏字段,並使jQuery,但我有點不確定如何請求選項名? – Christian 2012-02-11 13:33:39

+0

使用formcollection表單以及你的modelname模型,然後你可以簡單地做請求[numberinloop]; – davethecoder 2012-02-11 13:47:55

+0

我總是發現代碼越少越好,嘗試使用http://api.jquery.com/category/plugins/templates/,也許可能擺脫所有在一起的表,因爲你不需要他們真的只是一些好的CSS和你可以得到相同的佈局,但只有一半的代碼。當你從下拉列表中選擇一個模型只是一個想法 – davethecoder 2012-02-11 13:51:49

相關問題