2014-05-22 299 views
3

在我的.net mvc項目中,我試圖將最初傳入視圖的模型傳遞迴控制器。它每次都是空的。試圖將模型數據從視圖傳遞到控制器

查看代碼:

@model Shop.Models.ShoppingModel 
... 
@using (Html.BeginForm()) 
     { 
      @Html.HiddenFor(model => model.payment.cardNumber) 
      @Html.HiddenFor(model => model.payment.cvv) 
      @Html.HiddenFor(model => model.payment.expMonth) 
      @Html.HiddenFor(model => model.payment.expYear) 

      <div class="buttons"> 
       <a href="@Url.Action("Index", "Cart")"><input type="button" class="button" id="back" value="Continue Shopping"></a> 
       <input type="submit" value="Submit Order" class="button" /> 
      </div> 
     } 

和控制器代碼:

[HttpPost] 
    public ActionResult Finish(ShoppingModel sm) 
    { 

     string[] response = commitTransaction(sm.payment, false); 
     if (response[0] == "1") // transaction was approved! 
     { 
      //TempData["Message1"] = "Your order was placed for a total of " + sm.cartSummary.TotalCost.ToString("c") + " on your " + sm.payment.ccType.ToString() + "."; 
      TempData["Message2"] = "You will be receiving an email shortly with your receipt."; 
      //ViewBag.Message = new string[] { TempData["Message1"].ToString(), TempData["Message2"].ToString() }; 
     } 
     else 
     { 
      TempData["Message1"] = "There was an error processing your order. Our IT dept has been notified about this."; 
      //ViewBag.Message = new string[] { TempData["Message1"].ToString() }; 
     } 
     return RedirectToAction("Complete", "Cart"); 
    } 

的ShoppingModel:

public class ShoppingModel 
{ 
    [Required] 
    public CartSummaryModel.DeliveryModel delivery = new CartSummaryModel.DeliveryModel(); 
    [Required] 
    public CartSummaryModel.PaymentModel payment = new CartSummaryModel.PaymentModel(); 
    [Required] 
    public CartSummaryModel cartSummary = new CartSummaryModel(); 
    [Required] 
    public StudentModel student = new StudentModel(); 
    [Required] 
    public RootObject midas = new RootObject(); 

} 

出於某種原因,ShoppingModel中的變量PaymentModel在完成方法都無效每次。任何人都可以看到我做錯了什麼?

回答

3

模型改成這樣:

public class ShoppingModel 
{ 
    [Required] 
    public CartSummaryModel.DeliveryModel delivery { get; set; }; 
    [Required] 
    public CartSummaryModel.PaymentModel payment { get; set; }; 
    [Required] 
    public CartSummaryModel cartSummary { get; set; }; 
    [Required] 
    public StudentModel student { get; set; }; 
    [Required] 
    public RootObject midas { get; set; }; 

} 
+0

這就是它!我一直在爲此奮鬥了一段時間。非常感謝你! – dmikester1

相關問題