2017-07-04 28 views
1

在asp.net MVC獲取模型值,但傳遞查看沒有顯示。數據從控制器傳遞時不顯示

控制器

public ActionResult Index(plan plan1) 
     { 
      var myCharge = new StripeChargeCreateOptions(); 

      string apiKey = ""; 
      var stripeClient = new StripeClient(apiKey); 
      var planService = new StripePlanService(apiKey); 
      StripePlan response = planService.Get("1234"); 
      plan1.Amount = (response.Amount).ToString(); 

      return View(); 
     } 

視圖

<div> 
    @Html.TextBoxFor(m => m.Amount) 

</div> 

如何顯示內部文本框的數量值

+1

您需要在對象(模型)中解析視圖。 – Webbanditten

+1

**返回查看(plan1); **您錯過了數據。 –

+0

謝謝...... –

回答

2

希望您的視圖與Model類強有約束(實例是plan1)。所以,您需要在您的退貨聲明中指定您的型號

return View(plan1); 
+0

謝謝...... –

1

這是因爲你不及格興田模型對象返回查看,你需要通過實例plan1回到動作視圖,只需將您的最後一行動作代碼更改爲:

return View(plan1); 

視圖獲取有關從控制器操作傳遞的模型對象的信息,但您沒有將其傳回,因此View無法知道它需要使用對象狀態呈現視圖。

我希望它現在對你來說是合理的,因爲它不顯示你的數據。

+0

謝謝...... –

1

答案已經給出。您應該返回模型bij呼叫

return View(plan1); 

最好是將ViewModel而不是您的模型返回到您的視圖。

public class PlanViewModel 
    { 
    public plan plan1 { get; set; } 
    } 




public ActionResult Index(plan plan1) 
{ 
    var myCharge = new StripeChargeCreateOptions(); 

    string apiKey = ""; 
    var stripeClient = new StripeClient(apiKey); 
    var planService = new StripePlanService(apiKey); 
    StripePlan response = planService.Get("1234"); 
    plan1.Amount = (response.Amount).ToString(); 

    var viewModel = new PlanViewModel { 
     plan1 = plan1 
    }; 

    return View(viewModel); 
} 
相關問題