【发布时间】:2011-11-09 20:34:31
【问题描述】:
我的控制器如下所示:
public class PortefeuilleController : Controller
{
public ActionResult Create()
{
return View(new PortefeuilleViewModel{Saldo = 0.0});
}
}
我的创建视图如下所示:
@model PortefeuilleViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>PortefeuilleViewModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Naam)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Naam)
@Html.ValidationMessageFor(model => model.Naam)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Saldo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Saldo)
@Html.ValidationMessageFor(model => model.Saldo)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
我的 PortefeuilleViewModel 看起来像这样:
public class PortefeuilleViewModel
{
[DisplayName("Naam")]
[Required(ErrorMessage = "Gelieve een naam in te voeren")]
public string Naam { get; set; }
[DisplayName("Saldo")]
public double Saldo { get; set; }
}
我的问题在于“Saldo”字段。我想验证如下:
将该字段留空应该是有效的(因为我的代码隐藏会将“”更改为“0.0”并将该值保存在数据库中)。但是,例如,'19,99' 和 '19.99' 也应该被传递为有效。其余全部无效。
我真的不知道如何才能做到这一点。我已经找到这篇文章了:http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx 但这根本没有解决我的问题(第二部分)......
任何帮助将不胜感激。谢谢。
【问题讨论】:
标签: c# asp.net-mvc validation