lundi 14 mars 2016

How to list with column names and values ExpandoObject in mvc view ?

How to list created expandoobject and its values in mvc view?The Controller code as below.I am confused that is it possible ? How to get column names and its values?

public ActionResult List(string tablename)
    {
        DataTable dt = new DataTable();
        string connectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        using (SqlConnection con = new SqlConnection(connectionString))
        {

            con.Open();

            using (SqlCommand command = new SqlCommand("Select_"+tablename, con))
            {
                command.CommandType = CommandType.StoredProcedure;

                    using (SqlDataAdapter sda = new SqlDataAdapter(command))
                    {
                        sda.Fill(dt);
                    }

            }

        }
        var dns = new List<dynamic>();

        foreach (var item in dt.AsEnumerable())
        {

            IDictionary<string, object> dn = new ExpandoObject();

            foreach (var column in dt.Columns.Cast<DataColumn>())
            {
                dn[column.ColumnName] = item[column];
            }

            dns.Add(dn);
        }


        return View(dns);
    }





Aucun commentaire:

Enregistrer un commentaire