MVC 2 ViewModel绑定dropdownlist 和LIST 取值问题。
以下是我的代码,请各位指教。// view model
public class CheckoutViewModel
{
// Cart products summary
public List<Cart> CartItems { get; set; }
public List<SelectListItem> CardTypes { get; set; }
public decimal Total { get; set;}
}
//controller
public ActionResult Index()
{
//SSLSwitch.UseHTTPS();
CheckoutViewModel checkout = new CheckoutViewModel();
List<Cart> cart = new List<Cart>();
cart = shoppingCart.GetCartItems(Session.SessionID).ToList(); // 从数据库中取值
checkout.CartItems = cart;
checkout.Total = 0;
//Credit Card types
checkout.CardTypes = new List<SelectListItem>();
checkout.CardTypes.Add(new SelectListItem
{
Text = "Visa",
Value = "Visa",
Selected = true
});
checkout.CardTypes.Add(new SelectListItem
{
Text = "MasterCard",
Value = "MasterCard",
});
checkout.CardTypes.Add(new SelectListItem
{
Text = "Amex",
Value = "Amex"
});
// 因为POST表单中不能直接取到MODEL.CARDTYPE的值,只能使用TempData
TempData["CardType"] = checkout.CartTypes;
TempData["CartItems"] = checkout.CartItems;
return View(checkout);
}
[HttpPost]
public ActionResult Index(CheckoutViewModel checkout, string submit ,string total )
{..}
// model 只能取到Total的值,得不到CARTTYPE 和CARTITEMS 的值 --------------------编程问答-------------------- <%=Html.DropDownListFor(model => model.SendMode, ViewData["list"]as List<SelectListItem>, new { @class = "TextBox8" })%>
补充:.NET技术 , ASP.NET