views:

312

answers:

3

Hi, I have facing the following problem after the update.

I have a Model with Class level Validation plus property level validation in it. After updating to MVC 2 RC 2. The model validation fails on Model binding. What i actually understand that new mechanism trying to validate the model when you first request it or say on GET and it get null object exception during tryvalidatemodel Model binding call.

My Model is like this below

[Serializable]    
[MetadataType(typeof(InterestClaimMetaData))] //metadata with all properties level validation

//these validations fails when you request a page.
[DateComparison("DateA", "DateB", eDateComparitor.GreaterThan, 
    ErrorMessage = "Date A must be greater than B Date")]

[MutuallyExclusive("A", "B", ErrorMessage = "Please select either A or B field")]   

public class IE {
    public int ID { get; set; }
    public byte[] Updated { get; set; }
}

DataComparison and MutuallyExclusive overrides the validate function isvalid and check the validation but it fails trying to validate as first requested. dont know how to stop this happening as it should not validate model on get request; just attach the properties.

Only models without these class level validation works.

Please advise. Thanks

A: 

Seperate your action method in your controller in to two action methods. Mark one as GET and the other as POST. Then only apply the validation in the POST method. So, for example, if you currently have an method called Create that looks something like this...

public ActionResult Create(YourModel yourModel)
{
    // Some code in here to validate stuff
    // Some code in here to do stuff
    return RedirectToAction("Index");
}

Split this out in to two methods like this...

[HttpGet]
public ActionResult Create()
{
    return View();
} 

[HttpPost]
public ActionResult Create(YourModel yourModel)
{
    try
    {
        // Some code in here to validate stuff
        // Some code in here to do stuff
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}
Russell Giddings
A: 

Thanks for reply, I already had separate get and post operation. i will explain in more detail.

My Controller actually implements and overrides the base controller function to implementment wizard functionality

public class ICController : WizardDataController

now wizardDatecontroller code is mentioned below, actually whenever you request controller it will first goes to wizardDatacontroller and run the OnActionExecuting function before it executes Get or Post operation. I will than do TryUpdateModel and subsequantly trying validates all input and class level validation, hence fails. It meant to run when you click NExt button on the screen but in MVC 2 RC 2 its failing because trying to validate all properties.

public class WizardDataController : Controller where T : class, new() { public T wizardData;

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        wizardData = (SerializationUtils.Deserialize(Request.Form["wizardData"])
            ?? TempData["wizardData"] 
            ?? new T()) as T ;
        TryUpdateModel(wizardData);
        base.OnActionExecuting(filterContext);

    }

    protected override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        if (filterContext.Result is RedirectToRouteResult || TempData["ForceWizardDataHydration"] != null)
        {
            TempData["wizardData"] = wizardData;
            TempData["ForceWizardDataHydration"] = null;
        }
        base.OnResultExecuted(filterContext);
    }
}
Nasir
A: 

Good morning if you have found a way around this issue please let me know I am running into the same issue trying to implement a wizard-like workflow in my controller. Cannot stop the validations from firing even on a GET request.

Michael Reyeros