简体   繁体   中英

null reference MVC3

I have the following code in my MVC3 homecontroller's Index method. What I am trying is, getting values from my Resource File (.resx) and show them in a view.

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);
}

i am getting null reference error while debugging.

Any Help?

In my view I am trying something like this.

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

How do I solve it?

Try this:

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

OR

Create list in ResourcesTexts constructor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM