2012-07-24 138 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