Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
This is my webapi controller
C#
public class CategoryController : ApiController
    {
        List<category> category;

        public class DataResult
        {
            public List<category> Category { get; set; }
        }

        #region Categories
        public DataResult GetCategories()
        {
            DataResult result = new DataResult();
            
            try
            {
                using (MyDB db = new MyDB())
                {
                    category = db.Category.OrderBy(c => c.Name).ToList();

                }
                if (category == null)
                {
                    throw new HttpResponseException(
                           Request.CreateResponse(HttpStatusCode.NotFound));
                }
                else
                {
                    result.Category = category;
                   
                    return result;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }

This is my jquery calling in document.ready()

C#
$.ajax({
       url: "/api/Category",
       type: "GET",
       dataType: 'json',
       success: function (data) {
           alert('hai');
       },
       error: function (err) {
           document.write(data);
       }
   });

am accessing data from entityframework datacontext,but it is not displaying alert.if am passing static data then it is working.

here /api/ is foldername,Category is Api Controller.

It is calling the controller method,but alert is not raising,why?
Posted
Updated 9-Nov-13 1:57am
v5
Comments
Sampath Lokuge 9-Nov-13 9:55am    
Check with the Chrome dev Tool for whether is there any errors on json return ?

Have you stepped through the code ? Is there data to return ? If there is an error being returned, then you should show what the actual error is. In fact, you should use your debugger to see what it is, inside the compiler. It could be all sorts of things ( a broken EF model is one ). Why is your list of categories outside the method ? What is MyDB ? It's your databasecontext ? I'd wrap your data layer up a bit nicer than that...

You need to do some debugging first, then if you can't solve it, give us some info to work with.
 
Share this answer
 
On server side Json is a string. How do you transfer DataResult Class into Json String?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900