c# - mvc3 从接口(interface)反射(reflect)具体类的数据注释

标签 c# asp.net-mvc-3 data-annotations unboxing asp.net-mvc-views

我正在尝试从各种类型的 View 中抽象出我的 View 模型。整个编译没有问题,但我在“反射(reflect)”(正式称为拆箱)数据注释方面遇到问题。

我有一个界面:

public interface IPerson
{
    string FirstName { get;set;}
    string LastName {get;set;}
}

我有两个实现接口(interface)的类:

public class Employee : IPerson
{
    [Required]
    [Display(Description = "Employee First Name", Name = "Employee First Name")]
    public string FirstName {get;set;}

    [Required]
    [Display(Description = "Employee Last Name", Name = "Employee Last Name")]
    public string LastName {get;set;}

    public int NumberOfYearsWithCompany {get;set;}
}

public class Client : IPerson
{
    [Required]
    [Display(Description = "Your first Name", Name = "Your first Name")]
    public string FirstName {get;set;}

    [Display(Description = "Your last Name", Name = "Your last Name")]
    public string LastName {get;set;}

    [Display(Description = "Company Name", Name = "What company do you work for?")]
    public string CompanyName {get;set;}
}

人物编辑 View :views/Person/Edit as such:

@model IPerson

<div class="clear paddingbottomxxsm">
    <div class="editor-label">
        @Html.LabelFor(model => model.FirstName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.FirstName)
        @Html.ValidationMessageFor(model => model.FirstName)
    </div>
</div>
<div class="clear paddingbottomxxsm">
    <div class="editor-label">
        @Html.LabelFor(model => model.LastName)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.LastName)
        @Html.ValidationMessageFor(model => model.LastName)
    </div>
</div>

员工编辑 View :views/Employee/Edit:

@model Employee

Html.RenderAction("Edit", "Person", new { person = Model });

<div class="clear paddingbottomxxsm">
    <div class="editor-label">
        @Html.LabelFor(model => model.CompanyName)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.CompanyName)
        @Html.ValidationMessageFor(model => model.CompanyName)
    </div>
</div>    

PersonController 在哪里:

public ActionResult Edit(IPerson person)
{
    return PartialView(person);
}

一切都可以正常编译和渲染。但是,数据注释 正在丢失。

因此,Employee/Edit 的结果如下:

FirstName [textfield]

LastName [textfield]

What Company do you work for? [textfield] Company Name is a required field

是否有为具体类取消装箱这些数据注释的方法?

旁注

我尝试将 IPerson 显式转换为 Employee:

@model IPerson
@{
    var employee = (Employee)Model;
}
<div class="clear paddingbottomxxsm">
    <div class="editor-label">
        @Html.LabelFor(model => employee.FirstName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => employee.FirstName)
        @Html.ValidationMessageFor(model => employee.FirstName)
    </div>
</div>
<div class="clear paddingbottomxxsm">
    <div class="editor-label">
        @Html.LabelFor(model => employee.LastName)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => employee.LastName)
        @Html.ValidationMessageFor(model => employee.LastName)
    </div>
</div>

这样做使得需要名字但没有从标签中获取显示属性。

更新 在多次讨论这是否是拆箱之后,我还没有找到从(更基本的)具体类中获取数据注释的简单解决方案。在 View (或助手)中使用反射来获取具体类的数据注释确实会破坏简单性的目标。

我们有一些基本相同但必填字段和显示名称略有不同的 View 。如果我可以将 View 模型传递到接口(interface) View 并且它会计算出所需的字段和显示属性,那将非常方便。如果有人想出办法做到这一点,我们将不胜感激。

最佳答案

我遇到了同样的问题,TextBoxFor 助手没有生成正确的标记验证。

我能够解决它的方法是使用 TextBox 帮助器而不是 TextBoxFor 帮助器。

这是对我有用的部分片段

    @model Interfaces.Models.EntryPage.ICustomerRegisterVM

    <p>
        @Html.ValidationMessageFor(model => model.Department)
        @Html.TextBox(Html.NameFor(model => model.Department).ToString(), Model.Department)
    </p>

如您所见,我使用 Html.NameFor 帮助器从表达式中生成正确的名称,然后传入属性。使用这种方法,MVC 能够为实现被引用为 View 模型的接口(interface)的具体类成功生成正确的不显眼的验证标记。

我还没有为 LabelFor 或其他助手尝试过这种方法。但我希望结果是一样的。

请注意 Html.NameFor 助手在 MVC5 中可用

关于c# - mvc3 从接口(interface)反射(reflect)具体类的数据注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13093634/

相关文章:

c# - 如何将数据表转换为泛型

jquery - 从客户端检测到潜在危险的 Request.Path 值

.net - 如何在整个站点的 MVC 3 Razor View Engine 中定义使用?

c# - 将货币在格式字符串上右对齐

c# - GetExternalLoginInfoAsync null with OWIN in ExternalLoginCallback 除非已经登录谷歌

c# - 如何找到当前的可执行文件名?

asp.net-mvc - 带参数的 ASP.NET MVC 3 客户端验证

c# - 如何使用元数据类验证数据注释

.net - ModelState.IsValid 在使用数据注释时始终为真,MVC 作为 Web API

c# - 如何正确使用 LogonUser 从工作组客户端模拟域用户