Josh
Josh

Reputation: 13818

Performing validation asp.net mvc without using models

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.

Upvotes: 0

Views: 512

Answers (3)

Mahesh Velaga
Mahesh Velaga

Reputation: 21991

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");
    }
}

Upvotes: 1

user151323
user151323

Reputation:

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");
    }
}

Upvotes: 2

Kieran Senior
Kieran Senior

Reputation: 18220

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.

Upvotes: 0

Related Questions