2012-02-09 71 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

相关问题