2012-10-30 3 views
0

MVC3 homecontroller의 Index 메서드에 다음 코드가 있습니다. 내가 시도하고있는 것은 내 리소스 파일 (.resx)에서 값을 가져 와서보기에 표시하는 것입니다.null 참조 MVC3

private ResourceManager rm = null; 
private ResourcesTexts text; 

public ActionResult Index() 
{ 
    text = new ResourcesTexts(); 
    rm = new ResourceManager("Credit.SiteResources", Assembly.GetExecutingAssembly()); 
    var res = rm.GetResourceSet(CultureInfo.CurrentCulture, true, true); 

foreach (DictionaryEntry resource in res) 
{ 
    if (resource.Key.ToString().Count() == 14) 
    { 
    string x = resource.Value.ToString(); 
    text.myList.Add(x); 
    } 
} 

return View(text); 
} 

디버깅 중에 참조 오류가 발생합니다.

어떤 도움말이 필요합니까?

제 생각에는 이런 식으로 노력하고 있습니다.

@foreach(var x in Model.myList.Item) 
{ 
    <p>@x</p> 
} 

어떻게 해결할 수 있습니까?

+0

NullReferenceException이가에 목록을 만드시겠습니까? – jrummell

+0

text.myList.Add (x); – Aqua

+0

은 text.myList가 제대로 초기화 되었습니까? – msancho

답변

2

이 시도 :

text = new ResourcesTexts(); 
text.myList = new List<string>(); 

또는

ResourcesTexts 생성자

+0

예. 감사합니다. 검색된 값은 순서가 맞지 않습니다. 어떻게 LINQ를 사용하여 목록의 항목을 정렬 할 수 있습니까? – Aqua

+0

[Here] (http://stackoverflow.com/questions/188141/c-sharp-list-orderby-alphabetical-order)이 문제를 해결하는 데 도움이되는 또 다른 대답이 있습니다. – msancho