I have the following action in my controller:
[HttpGet]
public ActionResult Office(GestionOffice model)
{
ModelState.Clear();
model.Initialize();
return View(model);
}
I don't need in action by GET, validations are made. This would help me in the performance of the call by GET.
This would be my ideal case:
[HttpGet]
[NotValidateModel]
public ActionResult Office(GestionOffice model)
{
model.Initialize();
return View(model);
}
Thank you.
Edit
NotValidateModel
clarified that does not exist, would attribute the case to avoid validations.
The reason to move the model in action is to MOCK
the model
Edit II
I have my action with POST, I need is to receive in my action by GET, the model without validation, to successfully complete the testing of the action in the controller
[HttpGet]
[NotValidateModel]
public ActionResult Office(GestionOffice model)
{
model.Initialize();
return View(model);
}
[HttpPost]
[ActionName("Office")]
[NotValidateModel]
public ActionResult OfficePost(GestionOffice model)
{
if(ModelState.IsValid)
{
model.Save();
return RedirectToAction("List");
}
model.Initialize();
return View(model);
}
Edition on the solution of @Mark
As I in my view I have a few calls to actions, I had to create a key with the action and the controller.
Custom ModelMetaData
public class CustomModelMetaData : ModelMetadata
{
public CustomModelMetaData(ModelMetadataProvider provider, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
: base(provider, containerType, modelAccessor, modelType, propertyName)
{
}
public override IEnumerable<ModelValidator> GetValidators(ControllerContext context)
{
var itemKey = this.CreateKey(context.RouteData);
if (context.HttpContext.Items[itemKey] != null && bool.Parse(context.HttpContext.Items[itemKey].ToString()) == true)
{
return Enumerable.Empty<ModelValidator>();
}
return base.GetValidators(context);
}
private string CreateKey(RouteData routeData)
{
var action = (routeData.Values["action"] ?? null).ToString().ToLower();
var controller = (routeData.Values["controller"] ?? null).ToString().ToLower();
return string.Format("NoValidation_{0}_{1}", controller, action);
}
}
Filter
public class NoValidationAttribute : FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
var itemKey = this.CreateKey(filterContext.ActionDescriptor);
filterContext.HttpContext.Items.Add(itemKey, true);
}
private string CreateKey(ActionDescriptor actionDescriptor)
{
var action = actionDescriptor.ActionName.ToLower();
var controller = actionDescriptor.ControllerDescriptor.ControllerName.ToLower();
return string.Format("NoValidation_{0}_{1}", controller, action);
}
}
Edit filter
there may be a case of having a foreach in the main view to call several partial views with the attribute NoValidation
. in this case, includes a control to check for the key. since it includes the name of the controller and action in the key, this key is almost unique, it can only be repeated in the case described
public class NoValidationAttribute : FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
var itemKey = this.CreateKey(filterContext.ActionDescriptor);
if (!filterContext.HttpContext.Items.Contains(itemKey))
{
filterContext.HttpContext.Items.Add(itemKey, true);
}
}
private string CreateKey(ActionDescriptor actionDescriptor)
{
var action = actionDescriptor.ActionName.ToLower();
var controller = actionDescriptor.ControllerDescriptor.ControllerName.ToLower();
return string.Format("NoValidation_{0}_{1}", controller, action);
}
}