且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET MVC - 使用视图模型时,客户端验证

更新时间:2021-11-20 22:45:48

您需要将电子邮件属性添加到您的ProductViewModel。具有以下属性:

You need to add the Email property to your ProductViewModel. With the following attributes:

[DisplayName("Email")]
[Required(ErrorMessage = "Please enter email")]
public string email { get; set; }

那么这种模式传递到您的HttpPost控制器

Then pass this model into your HttpPost controller

和从内即填充您的命令类。

and populate your command class from within i.e.

[HttpPost]
public ActionResult Product(ProductViewModel model)
{

PreOrderProductCommand command = new PreOrderProductCommand();

command.Id = model.id;    
command.Email = model.email;

if (ModelState.IsValid)
{
    command.Process();
    return View("ThankYou");
}
else
    return Product(command.Id);
}