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 member and return to the angular js code to display them in a ng-table?
any idea on this?
I personally is a sucker for JSON. I would return a list with only the member properties that interest me, as a JSON object.
public JsonResult GetAllmembers()
{
int totalRecords;
var Members = ApplicationContext.Current.Services.MemberService.GetAll(0, int.MaxValue, out totalRecords);
var viewModel = new List<JsonViewModel>();
foreach (var member in Members)
{
viewModel.Add(new JsonViewModel()
{
Name = member.Name
});
}
return Json(viewModel, JsonRequestBehavior.AllowGet);
}
Sorry, did´nt test the code, it was more to give an idea off the concept.
But i can post the full code.
This code workes for me and returns a json object with all members:
Using:
using System.Web.Mvc;
using umbraco.cms.businesslogic.member;
using System.Collections.Generic;
using Umbraco.Web.Mvc;
SurfaceController:
namespace ExampleNamespace
{
public class GetMemberController : SurfaceController
{
public JsonResult GetAllMembers()
{
var members = Member.GetAll;
var viewModel = new List<JsonViewModel>();
foreach (var member in members)
{
viewModel.Add(new JsonViewModel()
{
LoginName = member.LoginName
});
}
return Json(viewModel, JsonRequestBehavior.AllowGet);
}
}
public class JsonViewModel
{
public string LoginName { get; set; }
}
}
You do not need to create a view model. You can just use Linq in order to get the data in the form you want.
using System.Linq;
using System.Web.Mvc;
using Umbraco.Core;
using Umbraco.Web.Mvc;
namespace MyData
{
public class GetMemberController : SurfaceController
{
public JsonResult GetAllMembers()
{
int totalRecords;
var Members = ApplicationContext.Current.Services.MemberService.GetAll(0, int.MaxValue, out totalRecords);
var viewModel = from m in Members
select new { Name = m.Name, Email = m.Email };
return Json(viewModel, JsonRequestBehavior.AllowGet);
}
}
}
public class GetAllMemberController : UmbracoApiController
{
public Returntype GetAllmembers()
{
int totalRecords;
var Members = ApplicationContext.Current.Services.MemberService.GetAll(0, int.MaxValue, out totalRecords);
foreach (var member in Members)
{
// store member
}
return Returntype_list ;
}
i was not getting the idea on Returntype and store member accordingly.
Try the code below. I have not tested so inform me if it does not work.
using System.Linq;
using System.Web.Http;
using Umbraco.Core;
using Umbraco.Web.WebApi;
namespace MyData
{
public class GetMemberApiController : UmbracoApiController
{
[HttpGet]
public object GetAllMembers()
{
int totalRecords;
var Members = ApplicationContext.Current.Services.MemberService.GetAll(0, int.MaxValue, out totalRecords);
var viewModel = from m in Members
select new
{
Name = m.Name,
Email = m.Email
};
return viewModel;
}
}
}
You can get the data using the following code.
$.ajax({
type: "GET",
async: false,
url: "/umbraco/api/GetMemberApi/GetAllMembers",
contentType: "application/json; charset=utf-8",
dataType: "json",
statusCode: {
200: function (data) {
console.log(data);
// Do whatever you want with your data
}
},
error: function () { }
});
MemberService GetAll
Hi,
I am trying to fetch all the member using Memberservice as below
what could be the best way to store member and return to the angular js code to display them in a ng-table? any idea on this?
Regards, Harshit
I personally is a sucker for JSON. I would return a list with only the member properties that interest me, as a JSON object.
Hi Dennis,
I am getting error on return.
it showing it has some invalid arguments.
any idea?
Sorry, did´nt test the code, it was more to give an idea off the concept. But i can post the full code.
This code workes for me and returns a json object with all members:
Using:
SurfaceController:
Action Url:
Result:
You do not need to create a view model. You can just use Linq in order to get the data in the form you want.
Hi Aristotelis ,
Can we do it with ApiController instead of surface controller?
Thanks,
Yes, I was trying using UmbracoApiController
i was not getting the idea on Returntype and store member accordingly.
can you please suggest ?
Thanks
Try the code below. I have not tested so inform me if it does not work.
You can get the data using the following code.
its working perfectly fine.
Thanks a lot :)
is working on a reply...