[英]DataFormatString: the field must be a number
我有这个模型:
[DisplayFormat(DataFormatString = "{0:c}", ApplyFormatInEditMode = true)]
public decimal SpotPrice { get; set; }
在我的详细信息视图中,价格显示为“ 25,00”。 当我尝试编辑价格时,该字段会预先填写数据库条目“ 25,00”。 当我立即单击保存时,出现以下错误: "The field SpotPrice must be a number."
有了这个问题中给出的建议,我在Web.Config中设置了我的全球文化:
<system.web>
<globalization culture="nl-NL"/>
</system.web>
我还尝试了许多不同的DataFormatStrings,但没有一个允许我摆脱该错误消息。
我使用SQL Server来存储价格。
编辑:更新了问题:我该怎么做才能使此价格字段正确验证?
编辑:这是我的Edit方法,可用于保存新价格:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "CampingSpotId,SpotName,FieldName,Surface,Wifi,Water,Sewer,Reserved,Booked,SpotPrice,Type")] CampingSpot campingSpot)
{
if (ModelState.IsValid)
{
_campingspotrepository.SetModified(campingSpot);
return RedirectToAction("Index");
}
return View(campingSpot);
}
SetModified方法执行此操作:
public void SetModified(CampingSpot campingSpot)
{
db.Entry(campingSpot).State = EntityState.Modified;
db.SaveChanges();
}
您必须在模型中写入[DataType(DataType.Currency)]
[DataType(DataType.Currency)]
public decimal SpotPrice { get;set;}
我在货币和日期格式上也遇到了同样的问题。 如果没有人有合适的解决方案,则解决方法是使SpotPrice成为字符串并在服务器端对其进行验证:
public string SpotPriceStr { get; set; }
在控制器内,转换为十进制,如果失败,则向ModelState添加错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.