asp.net mvc根据ascx文件的位置显示模板的关联资源文件

时间:2011-01-15 12:18:24

标签: asp.net-mvc localization

我正在努力减少项目中重复代码的数量。我们从视图模型中总结了很多视图。 viewmodels抽象一些相当模块化的数据。当显示这些东西时,模块化东西的结构总是相同的,然而文本根据托管显示模板的视图而改变。所以...

第1节中的

我可能有:

public class Section1ReportViewModel{
    [UiHint("Person")]
    [Display(ResourceType = typeof(Resources.Section1), Name="ReportedPerson")]
    public Person  ReportedPerson {get; set; } 

    [UiHint("Address")]
    [Display(ResourceType = typeof(Resources.Section1), Name="HomeAddress")]
    public Address HomeAddress {get; set; }

    [UiHint("Person")]
    [Display(ResourceType = typeof(Resources.Section1), Name="AssociatedPerson")]
    public Person  AssociatedPerson {get; set; } 
}

Views / Section1 / index.aspx中的报表视图可能会有:

@Model Section1ReportViewModel

@Html.DisplayFor(x=>x.ReportedPerson)
@Html.DisplayFor(x=>x.HomeAddress)
@Html.DisplayFor(x=>x.AssociatedPerson)

Mvc可以在Shared / DisplayTemplates文件夹中查找使用UiHint属性检索显示模板。

现在,所以我不会重新创建所有的观点和内容,在第2节我会:

public class Section2ReportViewModel{
    [UiHint("Person")]
    [Display(ResourceType = typeof(Resources.Section2), Name="ReportedPerson")]
    public Person  ReportedPerson {get; set; } 

    [UiHint("Address")]
    [Display(ResourceType = typeof(Resources.Section2), Name="HomeAddress")]
    public Address HomeAddress {get; set; }

    [UiHint("Person")]
    [Display(ResourceType = typeof(Resources.Section2), Name="AssociatedPerson")]
    public Person  AssociatedPerson {get; set; } 
}

和Views / Section2 / index.aspx中的视图

@Model Section1ReportViewModel

@Html.DisplayFor(x=>x.ReportedPerson)
@Html.DisplayFor(x=>x.HomeAddress)
@Html.DisplayFor(x=>x.AssociatedPerson)

作为其中一个显示模板(Person.ascx)的示例:

@Model Person 
<h1>@Html.Resource("Title")</h1>
<dl>
    @Html.DetailLineFor(x=>x.FullName)
    @Html.DetailLineFor(x=>x.DateOfBirth)
</dl>

现在......这就是谜题。我想在Shared / DisplayTemplates文件夹中保留这些共享视图。但我不想在Shared / DisplayTemplates / App_LocalResources文件夹中保留它们的资源文件。我想将资源文件保存在Views / Section2 / DisplayTemplates / App_LocalResources / Person.ascx.resx

的原因是使得用于h1标签文本将是特定于第2节,并且当相同的模板在第1次从文本使用/ SECTION1 / DisplayTemplates / App_LocalResources文件/ Person.ascx.resx将被使用。< / p>

有没有办法让这个工作?我觉得这个东西的搜索路径可以处理它,但显然不是:(

1 个答案:

答案 0 :(得分:0)

创建一个Html助手并不容易,因为传入的ctx是模板的,而且它不知道父视图。伪造Url无法传递给GetLocalResourceObject有两个原因:

1如果您正在使用区域,则模板上下文与其虚拟URL和物理URL有关。

2:你不能新建一个ResourceExpressionBuilder,并使用“假”网址调用资源表达式上的ParseExpression,试图让系统认为模板来自包含App_LocalResources的文件夹,其中包含所需的resx。

去内脏!!

作为另一个黑客,我将尝试使用链接项将模板添加到文件夹中。我会用我的调查结果回报。