2014-05-22 6 views
3

내 .net mvc 프로젝트에서 뷰에 원래 전달 된 모델을 다시 컨트롤러로 전달하려고합니다. 매번 null입니다.보기에서 컨트롤러로 모델 데이터를 전달하려고합니다

보기 코드 :

@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 : 마침 방법에 ShoppingModel 내부의 변수 PaymentModel가 null 몇 가지 이유로

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(); 

} 

때마다. 누구든지 내가 뭘 잘못하고 있는지 알 수 있니?

답변

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

관련 문제