2013-09-04 28 views
20

Tôi đang tìm kiếm một cách để vượt qua ViewDataDictionary để xem một phần trong ASP.NET MVC mà tôi đến cú pháp sau:Initializer cú pháp: mới ViewDataDictionary {{ "Tên", "Giá trị"}}

new ViewDataDictionary { { "Name", "Value" } } 

Tôi hơi bối rối về cú pháp khởi tạo ở đây. bất cứ ai có thể giải thích cho tôi?

+0

MSDN trên initializers bộ sưu tập trong [C#] (https://msdn.microsoft.com/en-us/library/bb384062.aspx) và [VB] (https://msdn.microsoft.com/en-us/library/dd293617.aspx) – KyleMit

Trả lời

40

ViewDataDictionary thực hiện IDictionary<string, object>.

IDictionary<string, object> về cơ bản là bộ sưu tập là KeyValuePair<string, object>.

Bộ khởi tạo ViewDataDictionary (dấu ngoặc nhọn bên ngoài) chứa một bộ ngoặc nhọn khác đại diện cho bộ khởi tạo KeyValuePair<string, object>.

Lý do điều này có thể được giải thích trong this answer.

Bạn có thể thêm nhiều mục bằng dấu phẩy tách KeyValuePair<string, object> initializers:

var data = new ViewDataDictionary { { "Name", "Value" }, { "Name2", "Value2" } }; 

Tương tự như:

var data = new ViewDataDictionary 
     { 
      new KeyValuePair<string, object>("Name", "Value"), 
      new KeyValuePair<string, object>("Name2", "Value2") 
     }; 

Về cơ bản, các dấu ngoặc nhọn bên trong là cú pháp tốt đẹp cho khởi KeyValuePair<string, object> đối tượng.

+0

Các mục có trong 'IDictionary ' của kiểu 'KeyValuePair '? – Mahmoodvcs

+0

@Mahmoodvcs có, 'IDictionary ' về bản chất là một tập hợp của 'KeyValuePair '. – Oliver

4

tôi giải quyết này sử dụng một phương pháp khuyến nông:

/// <summary> 
/// Use this extension method to create a dictionary or objects 
///  keyed by their property name from a given container object 
/// </summary> 
/// <param name="o">Anonymous name value pair object</param> 
/// <returns></returns> 
public static Dictionary<string, object> ToDictionary(this object o) 
{ 
    var dictionary = new Dictionary<string, object>(); 

    foreach (var propertyInfo in o.GetType().GetProperties()) 
    { 
     if (propertyInfo.GetIndexParameters().Length == 0) 
     { 
      dictionary.Add(propertyInfo.Name, propertyInfo.GetValue(o, null)); 
     } 
    } 

    return dictionary; 
} 

Và một phần mở rộng Helper Html:

/// <summary> 
/// When viewData is null, we just return null. Otherwise, we 
///  convert the viewData collection to a ViewDataDictionary 
/// </summary> 
/// <param name="htmlHelper">HtmlHelper provided by view</param> 
/// <param name="viewData">Anonymous view data object</param> 
/// <returns></returns> 
public static ViewDataDictionary vd(this HtmlHelper htmlHelper, object viewData) 
{ 
    if (viewData == null) return null; 

    IDictionary<string, object> dict = viewData.ToDictionary(); 

    //We build the ViewDataDictionary from scratch, because the 
    // object parameter constructor for ViewDataDictionary doesn't 
    // seem to work... 
    ViewDataDictionary vd = new ViewDataDictionary(); 
    foreach (var item in dict) 
    { 
     vd[item.Key] = item.Value; 
    } 

    return vd; 
} 

Sử dụng từ một tập tin dao cạo như:

@Html.Partial("~/Some/Path.cshtml", Model, Html.vd(new { SomeKey = SomeObj })) 
+1

Hoàn toàn quá mức, nhưng đó là thông tin tốt –