I have a simple website build in MVC4 I do not know how to use multiple models in a single view so Now I have two models used on one view so i made a ViewModel
public class viewModelVm
{
public List slider { get; set; }
public List Service { get; set; }
}
my service class and slider class is also shown below public class Service
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Image { get; set; }
public List Getdata()
{
String connectionString = "Data Source=ASPKCO-DANISH\\SQLEXPRESS1;Initial Catalog=PakeezaSodagran;Integrated Security=True;";
String sql = "SELECT * FROM Service";
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
var model = new List();
while (rdr.Read())
{
var service = new Service();
service.ID = Convert.ToInt32(rdr["ID"]);
service.Name = rdr["Name"].ToString();
service.Description = rdr["Description"].ToString();
service.Image = rdr["Image"].ToString();
model.Add(service);
}
return model;
}
}
}
my slider class is as below public class slider
{
public string src { get; set; }
public string title { get; set; }
}
public ActionResult Index()
{
Service sr=new Service();
viewModelVm vm = new viewModelVm();
vm.Service = sr.Getdata();
return View(vm);
}
My Service Index View is below
@model List
| ID | Name | Description | Image |
|---|---|---|---|
| @Service.ID | @Service.Name | @Service.Description | @Service.Image |
"'System.Collections.Generic.List' does not contain a definition for 'Service' and no extension method 'Service' accepting a first argument of type 'System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)"
Source Error:
|
Mark TaborPosted Sep 15, 2016, 3:50 AM
Vaibhav DeshmukhPosted Sep 15, 2016, 1:30 AM
Mark TaborPosted Sep 14, 2016, 12:42 PM
Vaibhav Deshmukhdear What changed you did in my code ? i mean where you changed the code , the code is same as i have ? what changes you made to run this code
Mark TaborPosted Sep 14, 2016, 11:59 AM
Vaibhav DeshmukhI am checking your modified code can you add me on skype danish.planpak is my skype address
Mark TaborPosted Sep 14, 2016, 11:58 AM
Vaibhav DeshmukhPosted Sep 14, 2016, 7:43 AM
Vasu GadhiyaPosted Sep 14, 2016, 6:56 AM