Reputation: 7369
Can I do this inside one class?
...
using System.Web.Mvc;
...
public static class aaa
{
public static test() {
if (something) ModelState.AddModelError("", "test");
}
}
inside controller call:
...
public class MyControler {
public void Index() {
....
aaa.test();
....
}
}
Is it possible? I want to control the code inside a classes in another part of the controller.
Upvotes: 18
Views: 9573
Reputation: 11
Works well in ASP.NET Core 2 :
//Controller
if (MyValidator.SaveValidation(model, ModelState))
{
myManager.Update(model);
return Json(new
{
responseText = "Saved"
});
}
//Validator
public static class SaveDossierValidator
{
public static bool SaveValidation(MyModel model, ModelStateDictionary modelState)
{
if (model.property == null) {
modelState.AddModelError("property", "Error message");
}
return modelState.IsValid;
}
}
Upvotes: 1
Reputation: 119066
You can pass your model state around like this:
public static void test(ModelStateDictionary ModelState)
{
if (something) ModelState.AddModelError("", "test");
}
And call from inside your controller:
aaa.test(ModelState);
Upvotes: 29
Reputation: 5832
No, you can't. What you're referencing inside Controller is its property, not a class. You can still pass it to the method being called as an argument.
Upvotes: 1