手动调用的ModelState验证 [英] Manually invoking ModelState validation

查看:653
本文介绍了手动调用的ModelState验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC 3 code-第一,我已经添加了验证数据来说明我的模型。下面是一个例子模型:

I'm using ASP.NET MVC 3 code-first and I have added validation data annotations to my models. Here's an example model:

public class Product
{
    public int ProductId { get; set; }

    [Required(ErrorMessage = "Please enter a name")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Please enter a description")]
    [DataType(DataType.MultilineText)]
    public string Description { get; set; }

    [Required(ErrorMessage = "Please provide a logo")]
    public string Logo { get; set; }
}

在我的网站我有一个多步骤的过程创建一个新的产品 - 第1步中输入产品的详细信息,步骤2中的其他信息等每一步我存储的每个对象(即产品对象)在会话之间,这样用户就可以回去的过程中这一阶段并修改它们输入的数据。

In my website I have a multi-step process to create a new product - step 1 you enter product details, step 2 other information etc. Between each step I'm storing each object (i.e. a Product object) in the Session, so the user can go back to that stage of the process and amend the data they entered.

在每个屏幕我有客户端验证与新的jQuery验证做工精细。

On each screen I have client-side validation working with the new jQuery validation fine.

的最后阶段是在此之后,产物被在数据库中创建一个确认屏幕。然而,由于用户可以阶段之间跳跃,我需要验证的对象(产品和其他一些人),以确定它们已正确完成的数据。

The final stage is a confirm screen after which the product gets created in the database. However because the user can jump between stages, I need to validate the objects (Product and some others) to check that they have completed the data correctly.

有没有办法以编程方式调用有数据注释的对象上的ModelState验证?我不希望要经过的每个属性的对象,做手工验证。

Is there any way to programatically call the ModelState validation on an object that has data annotations? I don't want to have to go through each property on the object and do manual validation.

我接受的如何,如果它使得它更容易使用ASP.NET MVC 3的模型验证功能提高了这一过程的建议。

I'm open to suggestions of how to improve this process if it makes it easier to use the model validation features of ASP.NET MVC 3.

推荐答案

您可以调用控制器内的 ValidateModel 动作(<一个href=\"http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k%28SYSTEM.WEB.MVC.CONTROLLER.VALIDATEMODEL%29;k%28VALIDATEMODEL%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK,VERSION=V4.0%22%29;k%28DevLang-CSHARP%29&rd=true\">documentation这里)。

这篇关于手动调用的ModelState验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆