0

我正在尝试使用 api 控制器 - 我将它连接到服务层以返回结果并且在设置时遇到了一些问题。这就是我所拥有的:

服务

public IEnumerable<RoleUser> GetUsers(int sectionID)
{
    var _role = DataConnection.GetRole<RoleUser>(sectionID, r => new RoleUser
    {
        Name = RoleColumnMap.Name(r),
        Email = RoleColumnMap.Email(r)
    }, resultsPerPage: 20, pageNumber: 1);
    return _role;
}

模型

public partial class RoleView
{
    public RoleView()
    {
        this.Users = new HashSet<RoleUser>();
    }
    public ICollection<RoleUser> Users { get; set; }
}

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

Api Controller 我应该连接 RoleUser 还是 RoleView 以及如何在此处设置我的数据以从服务中获取。

public class RoleApiController : ApiController
{
    public RoleUser GetRoleUser(int sectionID)
    {
        if (sectionID != null)
        {
            return new RoleUser
            {
               Name = ,
               Email = 
            };
        }
        throw new HttpResponseException(HttpStatusCode.NotFound);
    }
}

看法

<div>
Name: <span id="name"></span>
</div>
<div>
    Email: <span id="email"></span>
</div>
<script type ="text/javascript" src="~/Scripts/jquery-1.9.1.min.js"></script>
<script type ="text/javascript">
    getRoleUser(9, function (roleUser) {
        $("#name").html(roleUser.Name);
        $("#email").html(roleUser.Email);
    });
    function getRoleUser(id, callback) {
        $.ajax({
            url: "/api/RoleUser",
            data: { id: id },
            type: "GET",
            contentType: "application/json;charset=utf-8",
            statusCod: {
                200: function (roleUser) { callback(roleUser); },
                404: function () { alter("Not Found!"); }
            }
        });
    }
</script>
4

1 回答 1

0

您需要做的就是在控制器中为您的服务类添加一个字段:

public class RoleApiController : ApiController
{
    private RoleService _roleService = new RoleService();

    public RoleUser GetRoleUser(int sectionID)
    {
        if (sectionID != null)
        {
            return _roleService.GetUsers(sectionID);
        }
        throw new HttpResponseException(HttpStatusCode.NotFound);
    }
}
于 2013-03-28T19:16:02.560 回答