I have a really simple ASP.NET MVC website where users can browse products and place a pre-order. To place it they have to specify the name and email address. The controller looks like this:
[HttpGet]
public ActionResult Product(string id)
{
return View(new ProductViewModel(id));
}
[HttpPost]
public ActionResult Product(PreOrderProductCommand command)
{
if (ModelState.IsValid)
{
command.Process();
return View("ThankYou");
}
else
return Product(command.Id);
}
ProductViewModel
contains various product info (name, description, price, etc.) and PreOrderProductCommand
contains just 3 string
properties: Id
, Name
and Email
.
Now I want to enable client side validation of Name
and Email
using jquery.validate
and can't figure out how to implemet it. All the tutorials on the web say that I have to use code like this:
@Html.ValidationMessageFor(model => model.Email)
But the problem is that my view has ProductViewModel as a model and this class doesn't have the property Email
. This property and its validation logic reside in PreOrderProductCommand
.
What's the right way to implement client-side validation in this case? What am I missing?