1

I am trying to fetch all the member using Memberservice as below

 public  GetAllmembers()
{

    int totalRecords;
    var Members = ApplicationContext.Current.Services.MemberService.GetAll(0, int.MaxValue, out totalRecords);
    foreach (var member in Members)
    {
        //
    }
    return ;

}

what could be the best way to store a member and return to the angular js call to display them in a ng-table? any idea on this?

Regards,

Harshit
  • 397
  • 2
  • 6
  • 17
  • eliminate ajax, use angular $http services – Anonymous Duck Mar 08 '16 at 07:34
  • Hi, sorry i am using angular js call only. i am trying to return the data to that call but confused in returning data in generic list or jobject format.could you suggest the best way and little hint about it? – Harshit Mar 08 '16 at 07:49
  • update your service function to return json format and pass the list as JSOn – Anonymous Duck Mar 08 '16 at 07:51
  • Like this ' public JsonResult GetAllmembers() { int totalRecords; var Members = ApplicationContext.Current.Services.MemberService.GetAll(0, int.MaxValue, out totalRecords); var viewModel = new List(); foreach (var member in Members) { viewModel.Add(new JsonViewModel() { Name = member.Name }); } return Json(viewModel, JsonRequestBehavior.AllowGet); } ' – Harshit Mar 08 '16 at 07:59
  • or is there is better way? – Harshit Mar 08 '16 at 08:00
  • First of all, what is your backend implementation? are you using web api? – Anonymous Duck Mar 08 '16 at 08:06
  • I am using ApplicationContext.Current.Services.MemberService service .there are no tables involved in it. GetAll method gets all the member details using the service and returns the data to angular js call.there i am displaying onto ng--table. – Harshit Mar 08 '16 at 08:12
  • I mean what type of project does GetAllMembers resides? an mvc project? web api? so we can identify how do we connect the angular to your backend – Anonymous Duck Mar 08 '16 at 08:18
  • ohh got it .its MVC – Harshit Mar 08 '16 at 08:20
  • i would prefer if you use web api, even if MVC + angular is doable but still webapi is straight forward and sending of request is easy – Anonymous Duck Mar 08 '16 at 08:24
  • ok cool. do you have any example such calls? – Harshit Mar 08 '16 at 08:28
  • there are many tutorial out there like this http://www.dotnetcurry.com/aspnet/1049/crud-database-aspnet-webapi-angularjs just take time to read – Anonymous Duck Mar 08 '16 at 08:30

1 Answers1

1

You are using Umbraco therefore use UmbracoWebApi.

Create a controller method that returns your data. In its simplest form, if you want to return all members, you could do this:

using System.Collections.Generic;
using System.Web.Http;
using Umbraco.Core.Models;
using Umbraco.Web.WebApi;

namespace NameOfYourUmbracoWebsiteProject.Controllers.ApiControllers
{
    public class MyMemberController : UmbracoApiController
    {
        [HttpGet]
        public IEnumerable<IMember> GetAllmembers()
        {
            var memberService = ApplicationContext.Services.MemberService;
            return memberService.GetAllMembers();
        }

    }
}

The endpoint url for this method would be:

http://www.mywebsite.com/umbraco/api/MyMember/GetAllMembers

Create the above method and then test the url in a browser (you will see result as xml rather then JSON).

This would return all of your Members (as JSON if called from angular) which is probably not exactly what you want therefore you should probably create a Model that contains the properties for each member that you actually need and then return a collection of that.

For example:

public class MyCustomMember
{
    public string Name { get; set; }
    public string Email { get; set; }
}

And then change your controller method to return a collection of MyCustomMember. Again if the endpoint is called from angular, web api should return the collection as JSON

[HttpGet]
public List<MyCustomMember> GetAllmembers()
{
    var memberService = ApplicationContext.Services.MemberService;

    var listMyCustomMember = new List<MyCustomMember>();

    foreach (var member in memberService.GetAllMembers())
    {
        var myCustomMember = new MyCustomMember
        {
            Name = member.Name,
            Email = member.Email
        };
        listMyCustomMember.Add(myCustomMember);
    }

    return listMyCustomMember;
}

Any questions?

wingyip
  • 3,465
  • 2
  • 34
  • 52