2015-02-11 78 views
-1

如何將更多屬性添加到我的mvc列表中?現在即時將所有3個三個不同的值添加到「值」屬性「myList」。我希望列表myList具有如下屬性:Text = item,Value1 = item.resourceid,Value2 = allQuestionsid,Value3 = allCategoriesIDs。具有多個屬性的Mvc列表

List<SelectListItem> myList = new List<SelectListItem>(); 

    foreach (var item in searchList.resources) 
     { 
       string allQuestionIDs = String.Join(", ", item.associatedQuestions.Select(q => q.id.ToString())); 
       string allCategoriesIDs = String.Join(", ", item.associatedQuestions.Select(q => q.categoryid.ToString())); 

       myList.Add(new SelectListItem { Text = item.linktext.ToString(), Value = String.Join(", ", item.resourceid, "AllQuestionsID " + allQuestionIDs, "AllCategoriesID " + allCategoriesIDs) }); 
     } 
     return System.Web.Helpers.Json.Encode(myList); 

回答

0

是否有某些原因,您不能創建自己的類放入列表中?

類:

public class MySelectListItem 
{ 
    public string Text { get; set; } 

    public string Value1 { get; set; } 

    public string Value2 { get; set; } 

    public string Value3 { get; set; } 

    public string Value4 { get; set; } 
} 

列表:

var myList = new List<MyCustomSelectListItem>(); 

    foreach (var item in searchList.resources) 
    { 
    string allQuestionIDs = String.Join(", ", item.associatedQuestions.Select(q => q.id.ToString())); 
    string allCategoriesIDs = String.Join(", ", item.associatedQuestions.Select(q => q.categoryid.ToString())); 

    myList.Add(new MyCustomSelectListItem 
    { 
     Text = item.linktext.ToString(), 
     Value1 = String.Join(", ", item.resourceid, "AllQuestionsID " + allQuestionIDs, "AllCategoriesID " + allCategoriesIDs), 
     Value2 = "More Stuff", 
     Value3 = "Even More Stuff", 
     Value4 = "The End" 
    }); 
    } 
    return System.Web.Helpers.Json.Encode(myList); 
相關問題