вот код, который может хорошо работать при использовании модели в сервисе:
Модели:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace listTest.Models
{
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Age { get; set; }
public string Location { get; set; }
public string Sex { get; set; }
}
}
ViewModel:
using listTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace listTest.ViewModels
{
public class ViewModel
{
public List<Employee> EmployeeDataList { get; set; }
}
}
сервис:
using listTest.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Data;
using Newtonsoft.Json;
namespace listTest.Service
{
public class DBService
{
private readonly static string cnstr = ConfigurationManager.ConnectionStrings["connection1"].ConnectionString;
SqlConnection conn = new SqlConnection(cnstr);
public List<Employee> getData()
{
List<Employee> DataList = new List<Employee>();
string sql = @"select * from Employee";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Employee emp = new Employee();
emp.Id = Convert.ToInt32(dr["Id"]);
emp.Name = dr["Name"].ToString();
emp.Age = dr["Age"].ToString();
emp.Location = dr["Location"].ToString();
emp.Sex = dr["Sex"].ToString();
DataList.Add(emp);
}
}
catch (Exception e)
{
throw new Exception(e.Message.ToString());
}
finally
{
conn.Close();
}
return DataList;
}
}
}
контроллер:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using listTest.Service;
using listTest.ViewModels;
using Newtonsoft.Json;
using listTest.Models;
using Newtonsoft;
using Newtonsoft.Json.Linq;
namespace listTest.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult EmpList()
{
DBService service = new DBService();
ViewModel data = new ViewModel();
data.EmployeeDataList=service.getData();
return View(data);;
}
}
}
но теперь я сообщаю, что не могу использовать модель в Сервисе, поэтому я изменил код следующим образом:
Сервис
using listTest.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Data;
using Newtonsoft.Json;
namespace listTest.Service
{
public class DBService
{
private readonly static string cnstr = ConfigurationManager.ConnectionStrings["connection1"].ConnectionString;
SqlConnection conn = new SqlConnection(cnstr);
//public List<Employee> getData()
public string getData()
{
//List<Employee> DataList = new List<Employee>();
List<string> DataList = new List<string>();
string sql = @"select * from Employee";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
//Employee emp = new Employee();
//emp.Id = Convert.ToInt32(dr["Id"]);
//emp.Name = dr["Name"].ToString();
//emp.Age = dr["Age"].ToString();
//emp.Location = dr["Location"].ToString();
//emp.Sex = dr["Sex"].ToString();
//DataList.Add(emp);
string Id = dr["Id"].ToString();
string Name = dr["Name"].ToString();
string Age = dr["Age"].ToString();
string Location = dr["Location"].ToString();
string Sex = dr["Sex"].ToString();
string jsonList = "{"+"Id"+":"+Id+","+"Name"+":"+Name+","+"Age"+":"+Age+","+"Location"+":"+Location+","+"Sex"+":"+Sex+"}";
DataList.Add(jsonList);
}
}
catch (Exception e)
{
throw new Exception(e.Message.ToString());
}
finally
{
conn.Close();
}
//return DataList;
return JsonConvert.SerializeObject(DataList);
}
}
}
контроллер:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using listTest.Service;
using listTest.ViewModels;
using Newtonsoft.Json;
using listTest.Models;
using Newtonsoft;
using Newtonsoft.Json.Linq;
namespace listTest.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult EmpList()
{
DBService service = new DBService();
ViewModel data = new ViewModel();
//data.EmployeeDataList=service.getData();
//don't know how to do
data.EmployeeDataList = JsonConvert.DeserializeObject<Employee>(service.getData());
return View(data);
}
}
}
что я хочу - десериализовать список и сохранить его в данных. EmployeeDataList
как мне поступить? Пожалуйста, помогите мне
Служба обновления:
using listTest.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Data;
using Newtonsoft.Json;
using System.Dynamic;
namespace listTest.Service
{
public class DBService
{
private readonly static string cnstr = ConfigurationManager.ConnectionStrings["connection1"].ConnectionString;
SqlConnection conn = new SqlConnection(cnstr);
//public List<Employee> getData()
public string getData()
//public List<string> getData()
{
List<string> DataList = new List<string>();
//List<Employee> DataList = new List<Employee>();
//List<dynamic> DataList = new List<dynamic>();
string sql = @"select * from Employee";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
//Employee emp = new Employee();
//emp.Id = Convert.ToInt32(dr["Id"]);
//emp.Name = dr["Name"].ToString();
//emp.Age = dr["Age"].ToString();
//emp.Location = dr["Location"].ToString();
//emp.Sex = dr["Sex"].ToString();
//DataList.Add(emp);
dynamic myObj = new ExpandoObject();
myObj.Id = dr["Id"].ToString();
myObj.Name = dr["Name"].ToString();
myObj.Age = dr["Age"].ToString();
myObj.Location = dr["Location"].ToString();
myObj.Sex = dr["Sex"].ToString();
DataList.Add(myObj);
//string Id = dr["Id"].ToString();
//string Name = dr["Name"].ToString();
//string Age = dr["Age"].ToString();
//string Location = dr["Location"].ToString();
//string Sex = dr["Sex"].ToString();
//string jsonList = "{" + "Id" + ":" + Id + "," + "Name" + ":" + Name + "," + "Age" + ":" + Age + "," + "Location" + ":" + Location + "," + "Sex" + ":" + Sex + "}";
//DataList.Add(jsonList);
}
}
catch (Exception e)
{
throw new Exception(e.Message.ToString());
}
finally
{
conn.Close();
}
//return DataList;
return JsonConvert.SerializeObject(DataList);
}
}
}