使用角度js通过webapi检索的数据不完整,只检索一列

时间:2016-05-24 20:24:10

标签: c# angularjs stored-procedures asp.net-web-api

问题描述

大家好,我遇到的问题是使用带存储过程的angularjs通过webapi显示数据库表中的数据。 它仅从数据库表的第一列检索数据。 据我所知,我尝试了一切,如果有人有答案,请帮助我解决问题。我被打了一个星期...... 我展示了所需的所有细节,如下所示。如果有人需要更多信息,请告诉我......

studentPage.html

<!DOCTYPE html>
<html>
<head>
<script src="Scripts/jquery-1.10.2.js"></script>
<script src="Scripts/angular.js"></script>    
<title></title>
<meta charset="utf-8" />
</head>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

    <table border="1">
        <thead>
            <tr>
                <th>
                    Student Name
                </th>
                <th>
                    Student Age
                </th>
                <th>
                    Student Dob
                </th>
                <th>
                    Student Choice
                </th>
            </tr>
        </thead>
        <tbody >
            <tr ng-repeat="stds in Students">
                <td align="center" style="border: solid 1px #659EC7; 
padding: 5px;table-layout:fixed;">
                    <span style="color:#9F000F">{{stds.name}}</span>
                </td>

                <td align="center" style="border: solid 1px #659EC7; 
padding: 5px;table-layout:fixed;">
                    <span style="color:#9F000F">{{stds.age}}</span>
                </td>

                <td align="center" style="border: solid 1px #659EC7; 
padding: 5px;table-layout:fixed;">
                    <span style="color:#9F000F">{{stds.dob}}</span>
                </td>

                <td align="center" style="border: solid 1px #659EC7; 
padding: 5px;table-layout:fixed;">
                    <span style="color:#9F000F">{{stds.choice}}</span>
                </td>
            </tr>
        </tbody>
    </table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope,$http) {    
$http.get("http://localhost:54657/api/items").success(function (data) {
   $scope.Students = data;
    }
    );
});
</script>

</body>
</html>

studController.cs

using studDetails1.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace studDetails1.Controllers
{
public class studController : ApiController
{
    studEntities studObjApi = new studEntities();
    [HttpGet]
    [Route("api/items/")]
    public ICollection<Student> selectStudents()
    {            
        ICollection <Student> returnItems = studObjApi.sp_CRUD(null, null, null, null, 5).ToList();    ---->Error shows on this line.
        return returnItems;
    }

}

}

数据库表at1的存储过程

USE [angular1]
GO
/****** Object:  StoredProcedure [dbo].[sp_CRUD]    Script Date: 
5/25/2016     12:57:06 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sp_CRUD]
-- Add the parameters for the stored procedure here
(
@name varchar(50) NULL,
@age varchar(50) NULL, 
@dob varchar(50) NULL,
@choice varchar(50) NULL,
@operation int 
)
As
Begin
IF(@operation=1)
BEGIN
    IF NOT EXISTS (SELECT * FROM at1 WHERE name=@name)
        BEGIN
            insert into 
at1(name,age,dob,choice)values(@name,@age,@dob,@choice)
            Select 'Inserted' as results  
        END  
    ELSE  
        BEGIN  
            Select 'Exists' as results  
        END
END

ELSE IF(@operation=2)
BEGIN
    IF EXISTS (SELECT * FROM at1 WHERE name=@name)  
        BEGIN
            update at1 set name=@name,age=@age,dob=@dob,@choice=@choice 
where name=@name
            Select 'updated' as results                       
        END  
    ELSE  
        BEGIN  
            Select 'Exists' as results  
        END
END

ELSE IF(@operation=3)
BEGIN
    delete from at1 where name=@name
END

ELSE IF(@operation=4)
    BEGIN
        select [name],  
            [age],  
            [dob],  
            [choice] 
        from 
            at1
        WHERE  
            name =  @name
    END

ELSE IF(@operation=5)
    BEGIN
        select name,
                age,
                dob,
                choice
        from
            at1
    END
End

执行studentPage.html的图像

enter image description here

如上图所示,表格中没有数据显示。但是检索数据库表at1中第一列的数据,但不显示。 我通过直接给出api名称以下面的方式检查了它......

执行studentPage.html的XML图像

enter image description here

这是我到目前为止收集的细节,所以我非常非常需要这个,帮助我解决这个问题,并提前感谢:)

Student.cs

namespace studDetails1
{
public class Student
{
    public string name { get; set; }
    public string age { get; set; }
    public string dob { get; set; }
    public string choice { get; set; }
}
}

2 个答案:

答案 0 :(得分:0)

非常感谢您的示例帮助了我很多 @granadaCoder ,最后我将代码更改为以下方式并且有效。

public IHttpActionResult selectStudents() 
{ 
var data= studObjApi.SP_DISPLAY().ToList(); 
return Ok(data); 
}

答案 1 :(得分:-1)

变化:

   [HttpGet]
    [Route("api/items/")]
    public IList selectStudents()
    {            
        return studObjApi.sp_CRUD(null, null, null, null, 5).ToList();
    }

   [HttpGet]
    [Route("api/items/")]
    public ICollection<MyEntityName> selectStudents()
    {            
        /* forget all the stored procedure stuff for the moment */
        ICollection returnItems = new List<MyEntity>();
         MyEntityName e1 = new MyEntityName() { LastName = "Smith" };
         MyEntityName e2 = new MyEntityName() { LastName = "Jones" };
         MyEntityName e3 = new MyEntityName() { LastName = "Winkler" };
         returnItems.Add(e1);
         returnItems.Add(e2);
         returnItems.Add(e3);
         return returnItems;
    }

现在,您需要弄清楚MyEntityName是什么。你的程序创建了什么样的对象? 然后在那之后:

 [HttpGet]
    [Route("api/items/")]
    public ICollection<MyEntityName> selectStudents()
    {            
        /* forget all the stored procedure stuff for the moment */
        ICollection<MyEntityName> returnItems = studObjApi.sp_CRUD(null, null, null, null, 5).ToList();
        return returnItems; /* code it like this so you can set a breakpiont here */
    }

我认为你缺少创建DTO对象的整个概念(一个具有简单属性的小类......并返回那些ICollection。

您的MyObjectName可能是:

public class Student
{
  public string LastName {get;set;}
  public string FirstName {get;set;}
}