Logan Jones
Logan Jones

Reputation: 77

Add Data to a model

I have created a model with a List in it

namespace OMIv2._1KSWilson.Models
{
public class LegendClassVM
{
    public List <string> legendValues { get; set; }
}
}

then i have a foreach loop that creates strings that i want to use.

List<string> values = new List<string>();
        foreach (Feature f in allFeatures)
        {
            if (f.ColumnValues.ContainsKey(layercode))
            {
                if (!values.Contains(f.ColumnValues[layercode].ToString()))
                {
                    values.Add(f.ColumnValues[layercode].ToString());

                }
            }

        }

how would i add these items to the model list. this code executes five times so i need to store alot of data.

any suggestions would be greatly appreciated

Upvotes: 0

Views: 42

Answers (1)

Daniel Gpe Reyes
Daniel Gpe Reyes

Reputation: 417

try with this

public ActionResult Index()
    {
        LegendClassVM model  = new LegendClassVM();
        model.legendValues = new List <string>();

    foreach (Feature f in allFeatures)
    {
        if (f.ColumnValues.ContainsKey(layercode))
        {
            if (!values.Contains(f.ColumnValues[layercode].ToString()))
            {
              model.legendValues.Add(f.ColumnValues[layercode].ToString());

            }
        }

    }
        return View(model);
    }

Upvotes: 1

Related Questions