I am using a TextBoxFor to display an editable field. I need this field to display a value of “150” but allow the value to be changed.
I have tried using @Html.TextBoxFor(m => m.MembershipDues, new { @Value = "150" }) but this is not working for me.
Here are my Model’s Fields:
[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; }
Here’s my Action
[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();
}
I need for my textbox to display “150” and to be editable.
Any help will be greatly appreciated. Thanks in advance!
There’s a sometimes confusing issue where is you don’t specify your model with your view you won’t get default values set even if they are set using the Razor syntax.
Change
With
And see if that works.