我無法建立這個代碼models.views - - 缺少投我有一個API控制器,它經由模型服務層獲取數據,這是我所:無法隱式轉換類型 -
API控制器
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);
}
}
模型
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; }
}
錯誤消息:
無法隱式轉換類型System.Collections.Generic.IEnumberable < ... RoleUser>到RoleUser。一個顯式轉換存在(缺少強制轉換?)
這條線:return _roleService.GetUsers(sectionID);
的JavaScript
<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!"); }
}
success: function(result){
result.each(function())
}
});
}
</script>
[連接api控制器與服務層]可能的重複(http://stackoverflow.com/questions/15689989/connecting-api-controller-with-service-layer) – jrummell