如何验证从抽象类派生的ViewModels? [英] How to validate ViewModels derived from an abstract class?

查看:70
本文介绍了如何验证从抽象类派生的ViewModels?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下EditorTemplate

I have the following EditorTemplate

@model ESG.Web.Models.FileInfo // <-- Changed to BaseFileInfo
@{
    ViewBag.Title = "FileInfoEditorTemplate";
}
<fieldset>
    <table class="fileInfoEdit">
        <tr>
            <td>Base Directory:</td>
            <td>@Html.EditorFor(model => model.Directory)</td>
            <td>@Html.ValidationMessageFor(model => model.Directory)</td>
        </tr>
        <tr>
            <td>Filename:</td>
            <td>@Html.EditorFor(model => model.Filename)</td>
            <td>@Html.ValidationMessageFor(model => model.Filename)</td>
        </tr>
    </table>
</fieldset>

与此ViewModel对应的

which corresponds to this ViewModel

public class FileInfo
{
    [Display(Name = "Directory")]
    [Required(ErrorMessage="Please specify the base directory where the file is located")]
    public string Directory { get; set; }
    [Display(Name = "File Name")]
    [Required(ErrorMessage = "Please specify the name of the file (Either a templated filename or the actual filename).")]
    public string Filename { get; set; }

}

我想做的是重复使用上面的EditorTemplate,但是根据FileInfo类所使用的上下文自定义ErrorMessage.我可以使用标准文件名,例如abc.txt或"templated"文件名例如abc_DATE.txt,其中DATE将替换为用户指定的日期.在每种情况下,我都需要一个适当的错误消息.本质上,唯一的区别应该是注释.(我认为这是关键,但是不确定如何解决这个问题,因此我的方法很复杂!)

What I want to do is reuse the above EditorTemplate but customise the ErrorMessage based on the context the FileInfo class is used in. I can have a standard file name eg abc.txt or a 'templated' file name eg abc_DATE.txt where DATE will be replaced with some user specified date. I want an appropriate error message in each case. In essence, the only difference should be the Annotations. (I think this the key, but am not sure how to tackle this, thus my convoluted approach!)

我尝试创建抽象的基本视图模型,然后派生标准文件和模板化FileInfo类.我将当前EditorTemplate上的声明更改为

I have tried creating an abstract Base view model and then deriving a standard file and templated FileInfo classes. I change the declaration on the current EditorTemplate to

 `@model ESG.Web.Models.BaseFileInfo`  

并像

@Html.EditorFor(model => model.VolalityFile, "FileInfoEditorTemplate")` 

其中model.VolalityFileTemplatedFileInfo.这些值正确显示在编辑"页面上,但是,如果未正确填写字段,则不会进行客户端验证.我最初的猜测是,这与抽象类定义有关(在字段上没有任何注释).

where model.VolalityFile is a TemplatedFileInfo. The values are correctly displayed on the Edit page, however, there is no client-side validation when fields are not correctly filled. My initial guess is that this has something to do with the abstract class definition (not having any annotations on the fields).

public abstract class BaseFileInfo
{
    public abstract string Directory { get; set; }
    public abstract string Filename { get; set; }
}

// eg of derived class
public class TemplatedFileInfo : BaseFileInfo
{
    [Display(Name = "File Name")]
    [Required(ErrorMessage = "Please specify the name of a templated file eg someFileName_DATE.csv")]
    public override string Filename { get; set; }

    [Display(Name = "Directory")]
    [Required(ErrorMessage="Please specify the base templated directory where the file is located")]
    public override string Directory { get; set; }
}

这是我能想到的解决我需求的唯一方法,因此产生了一个问题-如何验证从抽象类派生的ViewModels?但是,如果还有另一种更可行的方法可以实现这一目标,请提出建议.

This is the only way I could think of to tackle my requirement, thus the question - How to validate ViewModels derived from an abstract class? However, if there is another more feasible way to achieve this, please advise.

推荐答案

我遇到了类似的情况,我不得不根据另一个属性的值来更改消息.

I had a similar situation where I had to change the message based the value of another property.

我制作了整个验证服务器端,没有注释.

I made the whole validation server side and without the annotations.

然后我以这种方式添加了ModelErrors:

Then I just added the ModelErrors this way:

ModelState.AddModelError("YourPropertyName", "The Error Message you want to show.);

这可能是多余的工作和过度杀伤力,但这确实帮了我大忙.

It's a bit extra work and overkill maybe but it did the trick for me.

这篇关于如何验证从抽象类派生的ViewModels?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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