I'm in a situation where I'd like to have two models in my view. I've been trying to do it as in the controller below, but I'm not sure how the proper way is to do it, but I'm assuming it's something with making an instance of the WellViewModel and then creating and adding to the respective lists, the WellViewModel contains but I think I've been muddling it up a little since I can't seem to get it to work. Any hints?
These are my models which I've put into a ViewModel:
public class WellViewModel
{
public List<WellModel> Wells { get; set; }
public List<AnnotationModel> Annotations { get; set; }
}
And this is where I retrieve the data from the database:
public ActionResult Well(string slideid)
{
WellViewModel model = new WellViewModel();
string cs = dbPath;
using (SQLiteConnection con = new SQLiteConnection(cs))
{
string stm = "SELECT * FROM Well WHERE SlideId = " + "'" + slideid + "'";
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
{
using (SQLiteDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
model.Wells = new List<WellModel>() { new WellModel()} //This is not possible to do
{
SlideId = rdr["SlideId"].ToString(),
Well = int.Parse(rdr["Well"].ToString()),
TimeStamp = rdr["TimeStamp"].ToString()
};
}
rdr.Close();
}
}
con.Close();
}
using (SQLiteConnection con = new SQLiteConnection(cs))
{
string stm = "SELECT * FROM Annotation WHERE SlideId = " + "'" + slideid + "'";
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
{
using (SQLiteDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
Annotations = new AnnotationModel
{
SlideId = rdr["SlideId"].ToString(),
Well = int.Parse(rdr["Well"].ToString()),
Par = rdr["Par"].ToString(),
Time = rdr["Time"].ToString(),
Val = rdr["Val"].ToString(),
TimeStamp = rdr["TimeStamp"].ToString()
};
}
rdr.Close();
}
}
con.Close();
}
return View(model);
}