Sina Sohi
Sina Sohi

Reputation: 2779

How do I save several set of information on my model class in MVC?

I'm making a simple website where I'm storing some information that I get from an excel file into my models class and retrieving them from the html page. The following class is a class in my models:

public class ToxinInformation
{
    public string cas_rn { get; set; }
    public string critical_effect { get; set; }
    public string point_of_departure { get; set; }
    public string adi_tdi { get; set; }
    public string intake { get; set; }
    public string hazard_quotient { get; set; }
    public string comment { get; set; }
    public string tox_link { get; set; }
    public string tox_link_decription { get; set; }
    public string intake_link { get; set; }
    public string intake_link_description { get; set; }

    public IList<string> Links { get; set; }
}

And I use this code to set the information in my controller class and return the view: (of course I would set information all the variables, not only the first one)

var model = new ToxinInformation
{
    cas_rn = "lol"
};
return View(model);

So far I can easily set all my strings and my list and retrieve them on my html page, but what do I do if in some cases I need several instances of the class "ToxinInformation"? In some cases I have 2 or more set of data I'd like to save and show in HTML except for just one.

Any suggestions would be very helpful.

Upvotes: 1

Views: 35

Answers (1)

Bill Gregg
Bill Gregg

Reputation: 7147

You should make a list and add instances of model to the list. Then you can use a DisplayFor or EditorFor template to show them all.

var models = new List<ToxinInformation>();

foreach(var dataBlob in YourDataStore)
{
   var model = new ToxinInformation()
   {
       cas_rn = dataBlob.cas_rn // Not sure where your raw data is coming from.
   }

   models.add(model)
}

return View(models);

Upvotes: 1

Related Questions