tags:

views:

77

answers:

3

Hello everyone, 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....

Would appreciate any help... Thanks.

+2  A: 

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");
    }
}
Developer Art
A: 

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.

Kezzer
Great article. But if he's not using models then he's prob not gonna want to create a whole service layer
cottsak
+1  A: 

@ New in Town : 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");
    }
}

PS: Sorry I can't comment as my reputation is below 50 points, so I had to post it as a answer.

Thanks, Mahesh Velaga.

Mahesh Velaga
Thank you. Fixed my answer.
Developer Art