传递参数时似乎没有调用Web方法

时间:2017-01-22 19:17:30

标签: c# html sql ajax

我正在尝试将参数从AJAX传递到我的[WebMethod],这样我就可以根据用户提供的ID返回SP的结果。

我遇到的问题是,只要我将参数名称添加到[WebMethod],我的控制器就不再被调用,这让我觉得我必须错误地编写[WebMethod]。

首先我们有HTML / AJAX,它没有问题,但返回所有数据库结果:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="Scripts/jquery-3.1.1.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#btnGetAllStudent').on('click', function() {
                var StudentID = $("#StudentID").val();
                $.ajax({
                    type: "POST",
                    url: "StudentTest.aspx/getAllStudent",
                    data: {"StudentID": StudentID},
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        $('#divStudentContainer').html('');
                        $.each(msg.d, function (index, item) {
                            studentIdElement = $('<span></span>').html(item.StudentId);
                            firstNameElement = $('<span></span>').html(item.FirstName);
                            lastNameElement  = $('<span></span>').html(item.LastName);
                            courseIdElement = $('<span></span>').html(item.CourseId);
                            $('#StudentName').val(item.StudentName);
                            $('#CourseName').val(item.CourseName);
                            $('#PhoneNo').val(item.PhoneNo);
                            $('#PhoneType').val(item.PhoneType);
                            $('#CourseStartDate').val(item.CourseStartDate);
                            $('#CourseEndDate').val(item.CourseEndDate);
                            $('#CourseDuration').val(item.CourseDuration);
                            $('#Remaining').val(item.Remaining);
                            $('#Elapsed').val(item.Elapsed);
                        });
                    },
                    error: function (msg) {
                        console.log(msg.responseText);
                    }
                });
            });
        });
    </script>
</head>
<body>
  <input type="button" id="btnGetAllStudent"
      name="btnGetAllStudent" value="Get All Students" />
    <div id="divStudentContainer"></div>

    <p>Enter StudentID:</p>
    <input type="text" id="StudentID" /> <br /><br />

    <p>StudentName:</p>
    <input type="text" id="StudentName" /> <br />

    <p>CourseName:</p>
    <input type="text" id="CourseName" /> <br />

    <p>PhoneNo:</p>
    <input type="text" id="PhoneNo" />  <br />

    <p>PhoneType:</p>
    <input type="text" id="PhoneType" />  <br />

    <p>CourseStartDate:</p>
    <input type="text" id="CourseStartDate" />  <br />

    <p>CourseEndDate:</p>
    <input type="text" id="CourseEndDate" />  <br />

    <p>CourseDuration:</p>
    <input type="text" id="CourseDuration" />  <br />

    <p>Remaining:</p>
    <input type="text" id="Remaining" />  <br />

    <p>Elapsed:</p>
    <input type="text" id="Elapsed" />  <br />
</body>
</html>

接下来我的[WebMethod]:

    namespace Student
    {
        public partial class StudentTest : System.Web.UI.Page
        {

            [WebMethod]
            public static List<object> getAllStudent(int StudentID)
            {
                StudentManager studentManager = new StudentManager();
                List<object> studentList = new List<object>();
                studentList = studentManager.getAllStudent();
                return studentList;
            }

And finally my controller:

namespace Student.StudentManagement.Domain
{
    public class StudentManager
    {
        public List<object> getAllStudent()
        {
            List<object> studentList = new List<object>();
            object student;

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString =   WebConfigurationManager.ConnectionStrings["StudentDB"].ToString();
            SqlCommand cmd = new SqlCommand("[dbo].[GetStudentData]", conn);
            cmd.Parameters.Add("StudentID", SqlDbType.Int);
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            cmd.Connection = conn;
            da.SelectCommand = cmd;

            conn.Open();
            da.Fill(ds, "StudentData");

            conn.Close();

            foreach(DataRow dr in ds.Tables["StudentData"].Rows)
            {
                student = new
                {
                    StudentName = dr["StudentName"].ToString(),
                    CourseName = dr["CourseName"].ToString(),
                    PhoneNo = dr["PhoneNo"].ToString(),
                    PhoneType = dr["Name"].ToString(),
                    CourseStartDate = dr["CourseStartDate"].ToString(),
                    CourseEndDate = dr["CourseEndDate"].ToString(),
                    CourseDuration = dr["CourseDuration"].ToString(),
                    Remaining = dr["Remaining"].ToString(),
                    Elapsed = dr["Elapsed"].ToString()

                };
                studentList.Add(student);
            }

            return studentList;
        }
    }
}

如果有人能指出我正确的方向,我会感激不尽,因为我花了好几个小时无处可去,所有这些都发生在我调用函数btnGetAllStudents网页冻结时。

**更新 - 对于缓慢的回复道歉,我的主板已经死了,我不得不按要求建造一台新的PC,SP:

USE [StudentRecords]
GO
/****** Object:  StoredProcedure [dbo].[GetStudentData]    Script Date: 29/01/2017 16:12:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      DB
-- Create date: 02/01/2017
-- Description: Return all students data
-- =============================================
ALTER PROCEDURE [dbo].[GetStudentData]

@StudentID INT

AS
BEGIN
    SELECT     ([S].[FirstName] + ' ' + [S].[LastName]) AS 'StudentName'
           ,[C].[CourseName]
           ,[P].[PhoneNo]
           ,[PT].[Name]
           ,[C].[CourseStartDate]
           ,[C].[CourseEndDate]
           ,CAST(DATEDIFF(MM,[C].[CourseStartDate],[C].[CourseEndDate]) 
                AS NVARCHAR(20)) + ' ' + 'Months'           AS 'CourseDuration'
           ,CAST(DATEDIFF(MM,GETDATE(),[C].[CourseEndDate])
                AS NVARCHAR(20)) + ' ' + 'Months Remaining' AS 'Remaining'
           ,CAST(DATEDIFF(MM,[C].[CourseStartDate], GETDATE())
                AS NVARCHAR(20)) + ' ' + 'Months Passed'    AS 'Elapsed' 

FROM        [dbo].[Student]   [S]
INNER JOIN  [dbo].[Course]    [C]  ON [C].[CourseID]     = [S].CourseID
INNER JOIN  [dbo].[Phone]     [P]  ON [P].[OwnerID]      = [S].[StudentID]
INNER JOIN  [dbo].[PhoneType] [PT] ON [PT].[PhoneTypeID] = [P].[PhoneTypeID]

WHERE       [StudentID] = @StudentID

SET NOCOUNT ON;

END

谢谢, 丹尼

0 个答案:

没有答案