I need help to format nullable decimal field as currency (with dollar sign and commas) in mvc application's razor view. Below is my modal and view code.
Model:
[Display(Name = "Eligible Amount")]
[RequiredIfProjectEngineerSelected("ProjectEngineerId", "", ErrorMessage = "Eligible Amount field is Required")]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal? EligibleAmount { get; set; }
View:
@{var formated = String.Format("{0:c}", decimal)decimal.Parse(@Model.project.ProjectTotalCost.HasValue ? @Model.project.ProjectTotalCost.Value.ToString() : ""));}
@Html.TextBoxFor(model => model.project.ProjectTotalCost, new { @Value = formatted})
It is displaying the formatted currency value in TextBoxFor control. But the problem what I am facing here is, when I am trying to update the value back, getting validation error saying "the value is not matching format".