将通用列表转换为IEnumerable列表以将其传递给View

时间:2019-05-09 06:53:45

标签: c# entity-framework model-view-controller

将通用列表转换为IEnumrable不适用于我

public DataTable GetDataTable(string query)
{
    string connString = ConfigurationManager.AppSettings["ConString"].ToString();
    DataTable dataTable = new DataTable();
    SqlConnection conn = new SqlConnection(connString);
    SqlCommand cmd = new SqlCommand(query, conn);
    conn.Open();

    // create data adapter
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    // this will query your database and return the result to your datatable
    da.Fill(dataTable);
    conn.Close();
    da.Dispose();
    return dataTable;
}

从数据库查询并传递给视图的通用方法

public IEnumerable<object> ListAll(object ObjEntity)
{     
    DataSet ds = new DataSet();
    List<object> objList = new List<object>();
    string TableName = ObjEntity.GetType().Name;
    string strSql = "SELECT * FROM " + TableName + ' ' + "where  delete_flag=0";
    var objmodel = GetDataTable(strSql);
    objList.Add(objmodel);
    return objList;
}

从列表中获取所有数据的操作方法

public ActionResult Index(Employees employee)
{
    var listEmp = dbContext.ListAll(employee);
    return View(listEmp);
}

0 个答案:

没有答案