当我进行我的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
}
什么是在FormCollection上的帖子? – 2012-02-09 21:05:33
请发布您现在的代码,我们会更容易看到问题。 – 2012-02-10 06:49:29
添加了som代码...只是说如果你需要更多的:) – Christian 2012-02-10 21:43:35