我有以下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>
对应于该视图模型
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而定制ErrorMessage
基础上,上下文FileInfo
类在使用。我可以有一个标准的文件名如abc.txt
或“模板”文件名如abc_DATE.txt
其中DATE
会与一些用户指定的日期替换。 我想在每种情况下相应的错误信息。 从本质上说, 唯一的区别应该是批注。 (我觉得这个关键,但我不知道如何解决这个问题,所以我费解的做法!)
我曾尝试创建一个抽象的基础视图模型,然后得出一个标准的文件和模板的FileInfo类。 我改变当前EditorTemplate的声明
`@model ESG.Web.Models.BaseFileInfo`
并使用它像
@Html.EditorFor(model => model.VolalityFile, "FileInfoEditorTemplate")`
其中model.VolalityFile
是TemplatedFileInfo
。 该值正确显示在编辑页面上,但是,没有客户端验证时字段不正确填写。 我最初的猜测是,这事做与抽象类定义(没有上的字段任何注释)。
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? 但是,如果有另一种实现这一目标更可行的办法,请指教。