我使用兩個單獨的頁面的局部視圖和局部視圖使用元數據來獲得對模型屬性的形式顯示名稱(執行元數據的標準方式行動)。MVC2發現面積/控制器/自定義屬性中
我需要根據網頁上的顯示名稱上下文敏感的。
爲此,我擴展了System.ComponentModel.DisplayNameAttribute,並傳入一個area/controller/action/resourcefile/resourcestring數組,以便根據上下文選擇正確的資源字符串。
我的問題是如何獲得的面積/控制器/動作從內執行以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonInterfaces.Helpers;
namespace CommonInterfaces.ComponentModel
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ContextSensitiveDisplayName : System.ComponentModel.DisplayNameAttribute
{
public class Context
{
public string Area { get; set; }
public string Controller { get; set; }
public string Action { get; set; }
public Type ResourceType { get; set; }
public string ResourceKey { get; set; }
public Context(string area, string controller, string action, Type resourceType, string resourceKey)
{
this.Area = area;
this.Controller = controller;
this.Action = action;
this.ResourceType = resourceType;
this.ResourceKey = resourceKey;
}
}
public ContextSensitiveDisplayName(params Context[] contexts)
{
/* Its these values that I need */
string currentArea = "";
string currentController = "";
string currentAction = "";
Context selectedContext =
contexts.FirstOrDefault(m =>
(m.Area == currentArea) &&
(m.Controller == currentController) &&
(m.Action == currentAction)
);
this.DisplayNameValue = ""; // Use the selectContext to retrieve string from resource file.
}
}
}
任何幫助,這將不勝感激。
我已經標記了這個,因爲你可以很容易地得到路由信息。 – 2011-01-14 11:06:21