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