troubleshooting Question

Display json result in html

Avatar of Sirdots
Sirdots asked on
12 Comments1 Solution467 ViewsLast Modified:
I have a method that returns json. Am using mvc 3. How can I write the html part to display my data. Here is my method and my Ajax script

        public JsonResult GetAllProducts(string searchName)       
            string myConnect = ConfigurationManager.ConnectionStrings["ConnectSir"].ConnectionString;
            List<Product> prdResults = new List<Product>();
            string sqlcmd = @"select * from products where name = @name";
            SqlConnection con = new SqlConnection(myConnect);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = sqlcmd;

            cmd.Parameters.Add("@name", SqlDbType.NVarChar);
            cmd.Parameters["@name"].Value = searchName;
            cmd.Parameters["@name"].Direction = ParameterDirection.Input;
            using (con)
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                    Product newProduct = new Product();
                    newProduct.Id = Convert.ToInt16(reader["Id"]);
                    newProduct.Name = reader["Name"].ToString();
                    newProduct.Description = reader["description"].ToString();
                    newProduct.Price = Convert.ToDecimal(reader["Price"]);
                    newProduct.UnitsInStock = Convert.ToInt16(reader["UnitsInStock"]);

            //return prdResults;
           return Json(prdResults);

<script type="text/javascript">
    $('#btnSearch').click(function () {
            url: 'Home/GetAllProducts/',
            type: 'POST',
            dataType: 'json',
            data: { searchName: $('#searchItem').val() } 
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 12 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros