我为GET
编写了一个基本CheckoutController
方法,用于返回视图,该视图将包含用于输入付款信息和促销代码的用户文本框。如何使用POST
方法访问此数据?
// GET: Checkout
public ActionResult PaymentsAndPromotions()
{
return View();
}
[HttpPost]
public ActionResult PaymentsAndPromotions()
{
var order = new Order();
try
{
if (db.Promotions !== PromoCode)
{
return View(order);
}
else
{
//save the order
db.Orders.Add(order);
db.SaveChanges();
//process the order
var cart = ShoppingCart.GetCart(this.HttpContext);
cart.CreateOrder(order);
return RedirectToAction("Complete", new object{id = order.OrderID});
}
}
catch
{
//invalid - redisplay with error
return View(order);
}
}
查看代码:
<form>
Payment Method:<br /><br />
Credit Card #: <br />
<input type="text" name="CreditCardNum" /><br />
Credit Card Type: <br />
<input type="text" name="CreditCardType" /><br/>
Promo Code: <br />
<input type="text" name="PromoCode"/> <br />
答案 0 :(得分:1)
我建议你创建一个这样的模型:
public class Order
{
public string CreditCardNum { get;set; }
public string CreditCardType { get;set; }
public string PromoCode { get;set; }
}
从控制器中执行此操作:
public ActionResult PaymentsAndPromotions()
{
var order = new Order();
return View(order);
}
[HttpPost]
public ActionResult PaymentsAndPromotions(Order order)
{
//you can get all your order's property here.
//example:
if (order.CreditCardNum != "test123")
{
}
return View(order);
}
在您看来,您可以执行以下操作(假设您使用的是razor语法):
@model Models.Order
@using (Html.BeginForm())
{
@Html.TextBoxFor(m => m.CreditCardNum)
@Html.TextBoxFor(m => m.CreditCardType)
@Html.TextBoxFor(m => m.PromoCode)
<input type="submit" value="Submit"/>
}
答案 1 :(得分:0)
adirks95 ,在控制器中,您可以使用以下代码获取表单数据:Request.Form["CreditCardNum"]
。如果您还有任何问题,请告诉我。