views:

139

answers:

2

I am using the MVC validation library from link text. I chose this library because I am also using .NetTiers which generates all of the Validation Attributes using MS Enterprise Library Validation Blocks.

It works fine except that that model binding is automatically validating the object and populating the Validation summary. I believe this in normal behavior.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Register([Bind()]NetTiersObject obj)
{
    return View();
}

The validation library also has a method that is documented as follows:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Register([Bind()]NetTiersObject obj)
{
    try
    {
        obj.Validate<NetTiersObject>();
    }
    catch (EntityValidationException ex)
    {
        ViewData.ModelState.PopulateWithErrors(ex);
    }
    return View();
}

This also works fine.

My problem is that when using the validation library's method it duplicates the error messages. When just using the model binding the error messages appear strange. The errors have the property name in the message.

So, I think I should either need to format the model binding error messages or disable model binding altogether.

Any recommendation, help?

Thanks.

A: 

Have you try:

    /*At the Point the ModelState should be Valid(TRUE)
      because we still didn't enforce any validations */
    var v = ModelState.IsValid;

try
    {
        obj.Validate<NetTiersObject>();
    }
    catch (EntityValidationException ex)
    {
        ViewData.ModelState.PopulateWithErrors(ex);
    }

And check if v is True, it should be. If it is true then obj.Validate<NetTiersObject>(); is doing something wrong because its the only populating the errors duplicated.

My problem is that when using the validation library's method it duplicates the error messages. When just using the model binding the error messages appear strange. The errors have the property name in the message.

Its is weird because the modelBinding should not populate Model errors, at least if your are not using a custom Model Binding or trying to save into the DB without validating fields.

Omar
In your first example it is Invalid right away. If I pass FormCollection as the action parameter the modelstate is valid. No custom modelbinding, and this is only a problem when validating. Thanks for your help.
Dustin Laine
A: 

I found the answer to why my ModelState was invalid. The form elements do not match up to the model, that is the only way that the ModelState would be invalid immediately.

If I find more specifics I will update this post.

Dustin Laine