我有一个修复订单域模型。以下是它的三个属性:
[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;在我搭建了Controller和视图之后,我修改了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>发布于 2015-12-31 19:18:15
当您在Value = string.Format("{0:C}", Model.LaborTotal)中执行htmlAttributes时,剃刀将执行C#代码并返回值"$86.34" (假设LaborTotal属性的值为86.34345M),它是一个字符串。因此,视图生成的标记将是
<input value="$86.34" id="LaborTotal" name="LaborTotal" readonly="readonly" type="text">省略了其他一些html属性。
因此,当您回发它时,字符串不能分配给十进制属性。您可以做的是从输入字段中移除货币符号,并将其显示在标签中。
<label>$</label>
@Html.TextBoxFor(model => model.LaborTotal,
new {@class = "form-control", @readonly = "readonly",
Value = string.Format("{0:0.00}", Model.LaborTotal)})如果您仍然希望点网为您提供货币符号而不是硬编码,则可以创建一个新的视图模型,该模型具有您的十进制属性的字符串版本,并使用它,当它回发时,您可以使用Parse方法重载之一将其转换为十进制。
此外,看起来您使用此字段仅用于显示目的(用户不编辑此值,在这种情况下,我建议只读取现有实体,只更新要更新的字段(在本例中,您不想更新LaborTotal)。
要做到这一点,并避免过度张贴,最好的解决方案是为视图创建一个视图模型。
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" />
}在您的帖子中,您将只更新您希望更新的属性。
[HttpPost]
public ActionResult View(LabelTotalSummaryVm model)
{
var e = yourDbContext.RepairOrders.FirstOrDefault(s=>s==Id);
if(e!=null)
{
vm.SomeEditableProperty =model.SomeEditableProperty ;
yourDbContext.Entry(e).State=EntityState.Modified;
yourDbContext.SaveChanges();
return RedirectToAction("SomeSuccess");
}
return View(vm);
}https://stackoverflow.com/questions/34550361
复制相似问题