Reputation: 2765
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);
}
Upvotes: 1
Views: 85
Reputation: 1206
I think it will be the best if you add a Constructor
to your WellViewModel
where you can set your WellModel List
an your AnnotationModel List
.
Like so:
public class WellViewModel
{
public WellViewModel()
{
Wells = new List<WellModel>();
Annotaions = new List<AnnotationModel>();
}
public List<WellModel> Wells { get; set; }
public List<AnnotationModel> Annotations { get; set; }
}
In your while loop then you can just add new WellModel
/ AnnotationModel
.
while (rdr.Read())
{
model.Wells.Add(new WellModel()
{
// set values here
});
}
Upvotes: 2
Reputation: 13248
In your while
loop, you create a new List<WellModel>
and add a single item.
Instead, define it like so:
WellViewModel model = new WellViewModel();
model.Wells = new List<WellModel>();
model.Annotations = new List<AnnotationModel>();
while (rdr.Read())
{
model.Wells.Add(new WellModel { // set values here };
}
Do the same for your other List
of Annotations
.
Upvotes: 3
Reputation: 56716
The only problem that I see with your code is improper use or reader to fill the lists with data. You should initialize a list outside the reading loop, and then just add new items to it:
model.Wells = new List<WellModel>();
using (SQLiteConnection con = new SQLiteConnection(cs))
{
....
while (rdr.Read())
{
WellModel well = new WellModel
{
SlideId = rdr["SlideId"].ToString(),
Well = int.Parse(rdr["Well"].ToString()),
TimeStamp = rdr["TimeStamp"].ToString()
};
model.Wells.Add(well);
}
Likewise for the other list.
Upvotes: 2