0

I've got a class which extends PartialViewResult and in this class I'm setting the Response.StatusCode and the Model via ViewData.

My model and statuscode are getting set as I'd expect, but my model isn't validated. Are their additional steps needed to ensure my model is validated? It feels like I've hooked into the wrong place (as in too late) in the pipeline and the model should already be validated before passing it to result but I'm not 100%.

Here's the class:

public class PartialViewResultWithErrorCode<T> : PartialViewResult where T : class
{
    private readonly HttpStatusCode statusCode;
    private readonly T model;

    public PartialViewResultWithErrorCode(HttpStatusCode statusCode, string viewName, T model)
    {
        this.statusCode = statusCode;
        this.model = model;
        ViewName = viewName;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.StatusCode = (int)statusCode;
        ViewData = new ViewDataDictionary(model);
        base.ExecuteResult(context);
    }
}

Any thoughts greatly appreciated.

dougajmcdonald
  • 19,231
  • 12
  • 56
  • 89

1 Answers1

0

It is probably too late. PartialViewResult get executed during InvokeActionResultWithFilters.

ModelValidation has occurred before that. You can create an ActionFilter and try to achieve the same thing in OnActionExecuting would give you a better result.

Spock
  • 7,009
  • 1
  • 41
  • 60