2

I'm using a third party component that I call a method passing a model object and if it finds any error, it will call a callback method passing an array of ValidationResult. What I wanna do is set those errors so that Html.TextBoxFor will render the attribute class="input-validation-error".

At the moment, I decorate my model class with the validation attributes that I need, and if the Name is empty, it works as expected, and make the Name input red, but if the component says it's invalid for any reason, I have a new foreach loop that will print the error messages...

I want to do something inside this callback method that will make my view work just like as if I had decorated my model class with the validation attributes...

Is this possible?

1 Answer 1

1

You can use ModelState.AddModelError(string, string) inside the controller.

// .. the model
public class MyModel { public string Property { get; set; } }

// .. in the controller
public ActionResult DoSomething(MyModel x)
{
    if(x.Property == "2")
    {
        ModelState.AddModelError("Property", "2 is not allowed");
        return View("EditorView", x);
    }
    // else....
}  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.