1

I am working on a web application using ASP.NET MVC 4 and I want to fetch data from database using AngularJS.

My issue is that I have two tables (products, category) like below:

public partial class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string imgurl { get; set; }
    public int CategoryId { get; set; }

    public virtual Category Category { get; set; }
}

public partial class Category
{
    public Category()
    {
        this.Products = new HashSet<Product>();
    }

    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Product> Products { get; set; }
}

I want to iterate data using ng-repeat from product table like this :

Name   category
================
iphone  -  Mobiles

Only, I can retrieve data like :

Name   category Id
================
iphone  -  1

I can’t retrieve category name from category table. but I can retrieve categoryId.

Is there any way to get data from database in this format using ng-repeat .

 Name   category
  ================
 iphone  -  Mobiles

I want to get data from two tables already between them relationship .

Thanks in advance.

4
  • 1
    What is your data access api ? Entity framework or else ? Commented Apr 25, 2016 at 14:07
  • can you show the query where you are using? Commented Apr 25, 2016 at 14:26
  • entity framework public JsonResult getAllProducts() { var products = db.Products.ToList(); return Json(products , JsonRequestBehavior.AllowGet); } Commented Apr 25, 2016 at 14:32
  • Wouldnt this be a server-side thing for how certain api requests are handled? Commented Apr 25, 2016 at 15:45

2 Answers 2

0

Please try as shown below (using Eager Loading)

public JsonResult getAllProducts() 
 { 
      var products = db.Products
                     .Include(p => p.Category)
                     .ToList();

      return Json(products , JsonRequestBehavior.AllowGet); 
 }

Update : You cannot retrieve collection of objects through the get method.You have to use post with the object collection as shown below.This is just a sample.Hope you can get the idea.

 public async Task<RefereeEditDto> GetRefereeForEditAsync(NullableIdInput<int> input)
        {
            var referee = await _refereeRepository.FirstOrDefaultAsync(p => p.Id == input.Id);
            var result = referee.MapTo<RefereeEditDto>();
            return result;
        }
Sign up to request clarification or add additional context in comments.

12 Comments

Unfortunately , dosen’t work and cant retrieve any data
What is the status of products when you put a debug there ? Are there any data on it with category or what ?
You have to go next step (next line).
when i inspect element in chrome, i got his error angular.js:13424 Error: [$rootScope:inprog] $digest already in progress http://errors.angularjs.org/1.5.3/$rootScope/inprog?p0=%24digest
is that mean do you have data for the category on above query when you do the debug ? You have to sort out that issue first.
|
0

Finally , i found the solution of my issue ... as follows:

query to join between two tables :

 public JsonResult getAllProducts() 
    {
        var products = from pr in db.Products
                       join ct in db.Categories
                       on pr.CategoryId equals ct.Id
                       select new
                       {
                           pr.Name,
                           pr.CategoryId,
                          CatName= ct.Name
                       };
            return Json(products , JsonRequestBehavior.AllowGet); 

                    }

after that , using ng-repeat to iterate data :

<tr ng-repeat="product in products" >
                <td>{{product.Name}}</td>
                <td>{{product.CatName}}</td>
                <td>{{product.CategoryId}}</td></tr>

Now , every things working fine and i can fetch data in which format i want... thanks to every one trying to help me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.