2012-07-24 53 views
0

我正在使用TextBoxFor來顯示可編輯的字段。我需要此字段顯示「150」的值,但允許更改值。如何在我的Html.TextBoxFor()中顯示默認值,該默認值可由用戶編輯?

我試過使用@Html.TextBoxFor(m => m.MembershipDues, new { @Value = "150" })但這不適合我。

這裏是我的模型的領域:

[Required] 
    [DataType(DataType.Text)] 
    public decimal MonthlyPayment { get; set; } 

    [Required] 
    [DataType(DataType.Text)] 
    public decimal MaintenanceFees { get; set; } 

    [Required] 
    [DataType(DataType.Text)] 
    public decimal MembershipDues { get; set; } 

    [Required] 
    [DataType(DataType.Text)] 
    public decimal ExchangeFees { get; set; } 

    [Required] 
    public string Duration { get; set; } 

    public decimal AnnualPayment { get; set; } 
    public decimal AnnualMaintenance { get; set; } 
    public decimal AnnualTotal { get; set; } 
    public decimal TenYearPayment { get; set; } 
    public decimal TenYearMaintenancePercentage { get; set; } 
    public decimal TenYearMembershipAndExchange { get; set; } 
    public decimal TenYearTotal { get; set; } 
    public decimal AnnualVacationCost { get; set; } 

這裏是我的行動

 [OutputCache(Duration = 60)] 
    public ActionResult CalculationView() 
    {   
     return View(); 
    } 

    [HttpPost] 
    [OutputCache(Duration = 60)] 
    public ActionResult Index(CalculaterModel cm) 
    { 
     if (ModelState.IsValid) 
     { 
      if (cm.Duration == "Annual") 
       cm.AnnualMaintenance = cm.CalculateMaintenanceFeesAnnually(cm.MaintenanceFees); 
      else if (cm.Duration == "Monthly") 
       cm.AnnualMaintenance = cm.CalculateMaintenanceFeesMonthly(cm.MaintenanceFees); 

      cm.AnnualPayment = cm.MonthlyPayment * 12; 
      cm.AnnualTotal = cm.AnnualPayment + cm.AnnualMaintenance; 
      cm.TenYearPayment = cm.AnnualPayment * 10; 

      decimal percentage = .08m; 
      decimal cost; 
      int i = 1; 
      decimal rate = cm.AnnualMaintenance; 
      while (i <= 10) 
      { 
       cost = rate * percentage; 
       rate = cost + rate; 
       cm.TenYearMaintenancePercentage = cm.TenYearMaintenancePercentage + rate; 
       i++; 
      } 
      decimal MDF = cm.MembershipDues + cm.ExchangeFees; 
      cm.TenYearMembershipAndExchange = MDF * 10; 
      cm.TenYearTotal = cm.TenYearMembershipAndExchange + cm.TenYearMaintenancePercentage + cm.TenYearPayment; 
      cm.AnnualVacationCost = cm.TenYearTotal/10; 
      return View(cm); 
     } 
     return View(); 
    } 

我需要爲我的文本框,顯示 「150」,併爲可編輯。 任何幫助將不勝感激。提前致謝!

+1

你能說清楚,當你說「不工作」,它不顯示,或不可編輯或不保存? – 2012-07-24 19:14:29

+0

您的操作方法中有很多業務邏輯...... – asawyer 2012-07-24 19:15:34

+0

@John Mitchell〜「不工作」表示當我需要顯示「150」時,初始TextBoxFor顯示「」。 – Bazinga 2012-07-24 19:20:11

回答

2

有一個有時令人困惑的問題,你沒有在視圖中指定模型,即使使用Razor語法設置它們,也不會獲得默認值集。

變化

return View(); 

隨着

var cm = new CalculaterModel(); 
return View(cm); 

,看看是否有效。

+0

我試過這個,並且沒有任何效果。 – Bazinga 2012-07-24 19:25:15

+0

在兩個地方都替換了View()?與查看(釐米)代碼?這個問題引起我的興趣的次數是瘋狂的(即默認視圖沒有模型,沒有默認顯示) – 2012-07-24 19:38:56

+1

非常感謝!那確實解決了這個問題。我的結局是一個noob錯誤。我在另一個視圖中調用了CalculationView,因此修正是將模型添加到調用CalculationView的視圖。再次感謝! – Bazinga 2012-07-24 19:49:05

相關問題