My controller looks like this:
public class PortefeuilleController : Controller
{
public ActionResult Create()
{
return View(new PortefeuilleViewModel{Saldo = 0.0});
}
}
My create view looks like this:
@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>
}
And my PortefeuilleViewModel looks like this:
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; }
}
My problem lies with the "Saldo" field. I want to validate it as follows:
Leaving the field empty should be valid (because my code-behind will change "" into "0.0" and save that value in the database). But, for example, both '19,99' and '19.99' should also be passed as being valid. All the rest is invalid.
I don't really know how I can pull this off. I already found this article: http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx But that didn't solve (the second part of) my problem at all...
Any help would be greatly appreciated. Thanks.