Serious n00b warning here; please take mercy!
So I finished the Nerd Dinner MVC Tutorial and I’m now in the process of converting a VB.NET application to ASP.NET MVC using the Nerd Dinner program as a sort of rough template.
I am using the “IsValid / GetRuleViolations()” pattern to identify invalid user input or values that violate business rules. I am using LINQ to SQL and am taking advantage of the “OnValidate()” hook that allows me to run the validation and throw an application exception upon trying to save changes to the database via the CustomerRepository class.
Anyway, everything works well, except that by the time the form values reach my validation method invalid types have already been converted to a default or existing value. (I have a “StreetNumber” property that is an integer, though I imagine this would be a problem for DateTime or any other non-strings as well.)
Now, I am guessing that the UpdateModel() method throws an exception and then alters the value because the Html.ValidationMessage is displayed next to the StreetNumber field but my validation method never sees the original input. There are two problems with this:
-
While the Html.ValidationMessage does signal that something is wrong, there is no corresponding entry in the Html.ValidationSummary. If I could even get the exception message to show up there indicating an invalid cast or something that would be better than nothing.
-
My validation method which resides in my Customer partial class never sees the original user input so I do not know if the problem is a missing entry or an invalid type. I can’t figure out how I can keep my validation logic nice and neat in one place and still get access to the form values.
I could of course write some logic in the View that processes the user input, however that seems like the exact opposite of what I should be doing with MVC.
Do I need a new validation pattern or is there some way to pass the original form values to my model class for processing?
CustomerController Code
// POST: /Customers/Edit/[id]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues)
{
Customer customer = customerRepository.GetCustomer(id);
try
{
UpdateModel(customer);
customerRepository.Save();
return RedirectToAction("Details", new { id = customer.AccountID });
}
catch
{
foreach (var issue in customer.GetRuleViolations())
ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}
return View(customer);
}
This would be a good starting point: Scott Gu – ASP.NET MVC 2: Model Validation
But I would suggest that it’s bad practice to databind stuff to your domain objects.
My personal approach would be to use POCO presentation models sprinkled with DataAnnotations validation attributes to do validation (this makes it easy to hook up client side validation later on). You can also create you own ValidationAttributes to hook into the databinding validation.
If it’s valid after databinding to your POCO object, pass it a service that does your more complex business validation. After that validation passes, pass it to another service (or the same service) that transfers the values to your domain object and then saves it.
I’m not familiar with the Linq to SQL GetRuleViolations pattern, so feel free to replace one of my steps with that pattern where it fits.
I’ll try my best to explain it here.
POCO presentation model:
Controller logic
Clear as mud? If you need more clarification, just let me know 🙂
Again, this is just my approach. You could possibly just follow the article by Scott Gu I linked to first and then go from there.
HTHs,
Charles