ASP.NET MVC Hierarchical Object List Business Rule Validation
I have a list of objects that are organized in a tree using a property Depth
:
public class Quota
{
[Range(0, int.MaxValue, ErrorMessage = "Please enter an amount above zero.")]
public int Amount { get; set; }
public int Depth { get; set; }
[Required]
[RegularExpression("^[a-zA-Z]+$")]
public string Origin { get; set; }
// ... another properties with validation attributes
}
Sample data (quantity - origin)
100 originA
200 originB
50 originC
150 originD
the model data looks like this:
IList<Quota> model = new List<Quota>();
model.Add(new Quota{ Amount = 100, Depth = 0, Origin = "originA");
model.Add(new Quota{ Amount = 200, Depth = 0, Origin = "originB");
model.Add(new Quota{ Amount = 50, Depth = 1, Origin = "originC");
model.Add(new Quota{ Amount = 150, Depth = 1, Orinig = "originD");
Editing a list
Then I use Variable Length List Editing, ASP.NET MVC 2-style , to bring up the list editing.
QuotaController.cs controller actions :
public class QuotaController : Controller
{
//
// GET: /Quota/EditList
public ActionResult EditList()
{
IList<Quota> model = // ... assigments as in example above;
return View(viewModel);
}
//
// POST: /Quota/EditList
[HttpPost]
public ActionResult EditList(IList<Quota> quotas)
{
if (ModelState.IsValid)
{
// ... save logic
return RedirectToAction("Details");
}
return View(quotas); // Redisplay the form with errors
}
// ... other controller actions
}
Show EditList.aspx :
<%@ Page Title="" Language="C#" ... Inherits="System.Web.Mvc.ViewPage<IList<Quota>>" %>
...
<h2>Edit Quotas</h2>
<%=Html.ValidationSummary("Fix errors:") %>
<% using (Html.BeginForm()) {
foreach (var quota in Model)
{
Html.RenderPartial("QuotaEditorRow", quota);
} %>
<% } %>
...
Partial view of QuotaEditorRow.ascx :
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Quota>" %>
<div class="quotas" style="margin-left: <%=Model.Depth*45 %>px">
<% using (Html.BeginCollectionItem("Quotas")) { %>
<%=Html.HiddenFor(m=>m.Id) %>
<%=Html.HiddenFor(m=>m.Depth) %>
<%=Html.TextBoxFor(m=>m.Amount, new {@class = "number", size = 5})%>
<%=Html.ValidationMessageFor(m=>m.Amount) %>
Origin:
<%=Html.TextBoxFor(m=>m.Origin)%>
<%=Html.ValidationMessageFor(m=>m.Origin) %>
...
<% } %>
</div>
Checking business rules
-
How to check a business rule : Should the amount of the quota be equal to the sum of the sums of its nested quotas (for example, 200 = 50 + 150 in the example)?
-
I want the corresponding inputs to
Html.TextBoxFor(m=>m.Amount)
be highlighted in red if a rule is violated for it. In the example, if the user enters not 200, but 201, it should be red on submit. -
Using only validation check.
Thanks a lot for the advice.
a source to share
Try FluentValidation . It is a very simple and powerful authentication system that is capable of validating your classes based on rules. It can also be used to create ClientSide JQuery Validators
The object validation result is a list of ValidationFailue objects. You can repeat them or bind to a repeater that contains an unordered list that is formatted in red.
IList<ValidationFailure> failures = results.Errors;
a source to share