0

カテゴリ テーブルのリストを返そうとしていますが、テーブルの最初の行のみが返されます。

var categories = response.Content.ReadAsAsync<IEnumerable<Categories>>().Result;

変数のカテゴリは次を返します。

[0] = Control.Models.Categories 
[1] = null 
[2] = null 
[3] = null

私のコントローラー:

HttpClient client = new HttpClient();
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["Url"]);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var id = Session["UserId"];
var url = "api/categoria/" + id;
HttpResponseMessage response = client.GetAsync(url).Result;
var categories = response.Content.ReadAsAsync<IEnumerable<Categories>>().Result;

モデル:

    [Key]
    public int intCategoriaPaiId { get; set; }

    [Key]
    public int intCategoriaOrdem { get; set; }

    [ForeignKey("intUsuarioId")]
    public virtual Usuario Usuario { get; set; }

    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int intCategoriaId { get; set; }

    public string strCategoriaNome { get; set; }

コントローラー Web API:

    // GET api/categories/1
    public IEnumerable<Categories> Get(int id)
    {
        var categories = repository.GetCategoriesByintUsuarioId(id);

        if (categories == null)
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        }
        else
        {
            return categories;
        }
    }

JSON:

  [{"$id":"1","intCategoriaPaiId":0,"intCategoriaOrdem":1,"intUsuarioId":1,"intCategoriaId":2,"strCategoriaNome":"Receitas","Usuario":{"$id":"2","intUsuarioId":1,"strUsuarioNome":"Jose Henrique","strUsuarioEmail":"jose.costa@hotmail.com","strUsuarioSenha":"KyPMH!ag","Categorias":[{"$ref":"1"},{"$id":"3","intCategoriaPaiId":2,"intCategoriaOrdem":1,"intUsuarioId":1,"intCategoriaId":3,"strCategoriaNome":"Salario","Usuario":{"$ref":"2"},"EntityKey":{"$id":"4","EntitySetName":"Categorias","EntityContainerName":"DatabaseContext","EntityKeyValues":[{"Key":"intCategoriaPaiId","Type":"System.Int32","Value":"2"},{"Key":"intCategoriaOrdem","Type":"System.Int32","Value":"1"},{"Key":"intUsuarioId","Type":"System.Int32","Value":"1"}]}},{"$id":"5","intCategoriaPaiId":3,"intCategoriaOrdem":1,"intUsuarioId":1,"intCategoriaId":4,"strCategoriaNome":"TESTE","Usuario":{"$ref":"2"},"EntityKey":{"$id":"6","EntitySetName":"Categorias","EntityContainerName":"DatabaseContext","EntityKeyValues":[{"Key":"intCategoriaPaiId","Type":"System.Int32","Value":"3"},{"Key":"intCategoriaOrdem","Type":"System.Int32","Value":"1"},{"Key":"intUsuarioId","Type":"System.Int32","Value":"1"}]}},{"$id":"7","intCategoriaPaiId":3,"intCategoriaOrdem":2,"intUsuarioId":1,"intCategoriaId":8,"strCategoriaNome":"Freela","Usuario":{"$ref":"2"},"EntityKey":{"$id":"8","EntitySetName":"Categorias","EntityContainerName":"DatabaseContext","EntityKeyValues":[{"Key":"intCategoriaPaiId","Type":"System.Int32","Value":"3"},{"Key":"intCategoriaOrdem","Type":"System.Int32","Value":"2"},{"Key":"intUsuarioId","Type":"System.Int32","Value":"1"}]}}],"EntityKey":{"$id":"9","EntitySetName":"Usuarios","EntityContainerName":"DatabaseContext","EntityKeyValues":[{"Key":"intUsuarioId","Type":"System.Int32","Value":"1"}]}},"EntityKey":{"$id":"10","EntitySetName":"Categorias","EntityContainerName":"DatabaseContext","EntityKeyValues":[{"Key":"intCategoriaPaiId","Type":"System.Int32","Value":"0"},{"Key":"intCategoriaOrdem","Type":"System.Int32","Value":"1"},{"Key":"intUsuarioId","Type":"System.Int32","Value":"1"}]}},{"$ref":"3"},{"$ref":"5"},{"$ref":"7"}]
4

1 に答える 1

0

問題を解決しました。モデルが間違っていた

モデルのカテゴリ:

[Key]
public int intCategoriaPaiId { get; set; }

[Key]
public int intCategoriaOrdem { get; set; }

[ForeignKey("Usuario")]
public int intUsuarioId { get; set; }
public Usuario Usuario { get; set; }

[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int intCategoriaId { get; set; }

public string strCategoriaNome { get; set; }

モデル ユーザーに欠けていたもの:

public virtual ICollection<Categoria> categorias { get; set; }
于 2013-09-18T20:24:57.843 に答える