如何将readonly @TextBoxFor Value正确格式化为货币?

时间:2015-12-31 18:32:02

标签: c# razor format

我有修理订单域模型。这里只有三个属性:

    [DataType(DataType.Currency)]
    [DisplayFormat(DataFormatString = "{0:C}", ApplyFormatInEditMode = true)]
    [DisplayName("Labor Total")]
    [ScaffoldColumn(true)]
    [ReadOnly(true)]
    public virtual decimal LaborTotal => Labor.Sum(p => p.LineTotal);

    [DataType(DataType.Currency)]
    [DisplayFormat(DataFormatString = "{0:C}", ApplyFormatInEditMode = true)]
    [DisplayName("Parts Total")]
    [ScaffoldColumn(true)]
    [ReadOnly(true)]
    public virtual decimal PartsTotal => RepairOrderParts.Sum(p => p.LineTotal);

    [DataType(DataType.Currency)]
    [DisplayFormat(DataFormatString = "{0:C}", ApplyFormatInEditMode = true)]
    [DisplayName("Order Total")]
    [ScaffoldColumn(true)]
    [ReadOnly(true)]
    public virtual decimal OrderTotal => LaborTotal + PartsTotal;

在我对控制器和视图进行了脚手架之后,我修改了Edit.cshtml,因此它将包含我的三种货币属性:

    <div class="form-group">
        @Html.LabelFor(model => model.LaborTotal, htmlAttributes: new {@class = "control-label col-md-2"})
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.LaborTotal, new {@class = "form-control", @readonly = "readonly", Value = string.Format("{0:C}", Model.LaborTotal) })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.PartsTotal, htmlAttributes: new {@class = "control-label col-md-2"})
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.PartsTotal, new {@class = "form-control", @readonly = "readonly", Value = string.Format("{0:C}", Model.PartsTotal) } )
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.OrderTotal, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.OrderTotal, new { @class = "form-control", @readonly = "readonly", Value = string.Format("{0:C}", Model.OrderTotal) } )
        </div>
    </div>

如果我删除该行的Value = string.Format…部分。所有版本都显示正常,我可以进行更改并保存记录。保留Value = string.Format…后,我的值格式正确但我无法保存任何更改。它抱怨所有三行都不是数字(没有逗号或美元符号)。我拿出了验证线,但没有帮助,也没有将它切换到@EditorFor

提前致谢,

修改

工作但未格式化货币:

    <div class="form-group">
        <label class="control-label col-md-2" for="LaborTotal">Labor Total</label>
        <div class="col-md-10">
            <input class="form-control" data-val="true" data-val-number="The field Labor Total must be a number." data-val-required="The Labor Total field is required." id="LaborTotal" name="LaborTotal" readonly="readonly" type="text" value="75.00" />
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-md-2" for="PartsTotal">Parts Total</label>
        <div class="col-md-10">
            <input class="form-control" data-val="true" data-val-number="The field Parts Total must be a number." data-val-required="The Parts Total field is required." id="PartsTotal" name="PartsTotal" readonly="readonly" type="text" value="0" />
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-md-2" for="OrderTotal">Order Total</label>
        <div class="col-md-10">
            <input class="form-control" data-val="true" data-val-number="The field Order Total must be a number." data-val-required="The Order Total field is required." id="OrderTotal" name="OrderTotal" readonly="readonly" type="text" value="75.00" />
        </div>
    </div>

尝试格式化货币:

    <div class="form-group">
        <label class="control-label col-md-2" for="LaborTotal">Labor Total</label>
        <div class="col-md-10">
            <input Value="$75.00" class="form-control" data-val="true" data-val-number="The field Labor Total must be a number." data-val-required="The Labor Total field is required." id="LaborTotal" name="LaborTotal" readonly="readonly" type="text" value="75.00" />
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-md-2" for="PartsTotal">Parts Total</label>
        <div class="col-md-10">
            <input Value="$0.00" class="form-control" data-val="true" data-val-number="The field Parts Total must be a number." data-val-required="The Parts Total field is required." id="PartsTotal" name="PartsTotal" readonly="readonly" type="text" value="0" />
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-md-2" for="OrderTotal">Order Total</label>
        <div class="col-md-10">
            <input Value="$75.00" class="form-control" data-val="true" data-val-number="The field Order Total must be a number." data-val-required="The Order Total field is required." id="OrderTotal" name="OrderTotal" readonly="readonly" type="text" value="75.00" />
        </div>
    </div>

1 个答案:

答案 0 :(得分:1)

当您在htmlAttributes中执行Value = string.Format("{0:C}", Model.LaborTotal)时,razor将执行C#代码并返回值"$86.34",(假设您的LaborTotal属性的值为{ {1}} )这是一个字符串。因此,您的视图生成的标记将为

86.34345M

省略了其他一些html属性

因此,当您将其发回时,该字符串无法分配给小数属性。您可以做的是从输入字段中删除货币符号并将其显示在标签中。

<input value="$86.34"  id="LaborTotal" name="LaborTotal" readonly="readonly" type="text">

如果您仍然希望dot net为您提供货币符号而不是硬编码,您可以创建一个新的视图模型,它具有您的小数属性的字符串版本并使用它,当它被回发时,您可以转换它使用<label>$</label> @Html.TextBoxFor(model => model.LaborTotal, new {@class = "form-control", @readonly = "readonly", Value = string.Format("{0:0.00}", Model.LaborTotal)}) 方法重载之一。到十进制。

此外,看起来您仅将此字段用于显示目的(用户未编辑此值,在这种情况下,我建议您阅读现有实体并仅更新您要更新的字段(在这种情况下,您不想更新LaborTotal)。

要做到这一点,并避免重叠,最好的解决方案是为您的视图创建一个视图模型

Parse

在你的GET行动中

public class LabelTotalSummaryVm
{
  public int Id {set;get;}
  public decimal LaborTotal {set;get;}      
  public string SomeEditableProperty { set;get;}
}

在您看来,

public ActionResult View(int id)
{
  var vm = new LabelTotalSummaryVm { Id=id };
  var e = yourDbContext.RepairOrders.FirstOrDefault(s=>s==Id);
  if(e!=null) 
  {
     vm.LaborTotal =e.LaborTotal ;
     vm.SomeEditableProperty =e.SomeEditableProperty ;
  }
  return View(vm);
}

在您的帖子中,您只会更新您要更新的媒体资源

@model LabelTotalSummaryVm
@using(Html.BeginForm())
{
  @Html.TextBoxFor(s=>s.SomeEditableProperty);
  <label>$ @Model.LaborTotal</label>
  @Html.HiddenFor(s=>s.Id)
  <input type="submit" />
}