當我進行我的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