0

How do I do validation in mvc if I'm not using models?

I'm directly obtaining data from the controller and displaying it.

How do I validate? Most examples seem to use the model to validate.

3 Answers 3

2

Although it is considered to be against MVC paradigm, nothing technically prevents you from working with the posted form directly.

class TestController : Controller
{
    [AcceptVerbs (HttpVerbs.Post)]
    public ActionResult SomeAction (FormCollection form)
    {
        if (MyCustomValidation (form))
            SaveData ();

        RedirectToAction ("SomeAction");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I think you might want to have [AcceptVerbs(HttpVerbs.post)] in your code:

class TestController : Controller
{
    [AcceptVerbs (HttpVerbs.Post)]
    public ActionResult SomeAction (FormCollection form)
    {
        if (MyCustomValidation (form))
            SaveData ();

        RedirectToAction ("SomeAction");
    }
}

Comments

0

You could use a service layer as described by this article, this allows both separation of concerns whilst maintaining error handling, not relying on the controller to do it all for you.

1 Comment

Great article. But if he's not using models then he's prob not gonna want to create a whole service layer

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.