i want to create a viewmodel to access in my view to two differents models i have created.
For this i create two diferrents models and one model which i include both.
My problem is that in my view i can´t access the data.
hope that anybody can help with this.
What i need to represented in my view are: Table1 : name title
Table2: picpath of each image
here is my code:
model 1:
public class Table1
{
public int ID { get; set; }
public string name{ get; set; }
public string title { get; set; }
public string edition{ get; set; }
public string number{ get; set; }
}
public class DefaultConnection : DbContext
{
public DbSet<Table1> Res{ get; set; }
}
model 2:
public class Images
{
public SelectList ImageList { get; set; }
public int ID { get; set; }
public string title{ get; set; }
public string picpath { get; set; }
public Img)
{
ImageList = GetImages();
}
public SelectList GetImages()
{
var list = new List<SelectListItem>();
string connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (var con = new SqlConnection(connection))
{
con.Open();
using (var command = new SqlCommand("SELECT * FROM Myimages", con))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string title = reader[1] as string;
string imagePath = reader[2] as string;
list.Add(new SelectListItem() { Text = title, Value = imagePath });
}
}
con.Close();
}
return new SelectList(list, "Value", "Text");
}
}
MY VIEW Model:
public class ViewModel
{
public Table1 table1{ get; set; }
public Images xpto { get; set; }
public ViewModel(Table1 table1)
{
Table1 = table1;
xpto = new Images();
}
}
**Controller:**
public ActionResult HotSpotMaker(int id = 0)
{
Table1 rev = db.Res.Find(id);
if (rev == null)
{
return HttpNotFound();
}
//Here is something missing, have delete my version here because don´t make any sense
return View(rev);
}
View:
@model myproject.Models.ViewModel
Note: i search a lot, and find that a lot of people use this: @model myproject.Web.Models.ViewModel , but i can´t select this web. . i don´t know if this is relevant or not, i thought maybe is important to say it.