搜索函数存储过程返回空结果

时间:2017-02-14 18:04:07

标签: sql-server stored-procedures

我正在尝试在网页上实现存储过程。最初,一个大型数据表加载到屏幕底部。在顶部,用户可以输入三个可用输入来搜索数据表:名称,课程和类型。

但是每次输入(正确)都传递给存储过程时,我会收到一个空对象。我确定问题出在存储过程中。

任何帮助都会很棒。感谢

SQL

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[spAssessmentDefinitionTestSearch] 
    @AssessmentName         NVARCHAR(50),
    @CourseIdList           XML,
    @AssessmentMethodIdList XML,
    @UserId                 INT,
    @ClientId               INT
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT 
        ad.Id, ad.Name,
        am.Name AS Method,
        ad.GradebookMaxPoints,
        c.Name AS Course,
        (SELECT COUNT(*)
         FROM Template t
         JOIN TemplateSection ts ON t.Id = ts.TemplateId
         JOIN TemplateSectionItem tsi ON ts.Id = tsi.TemplateSectionId
         WHERE t.Id = adt.TestTemplateId
           AND ts.IsActive = 1
           AND tsi.IsActive = 1) AS QuestionCount
    FROM 
        dbo.AssessmentDefinition ad
    JOIN 
        dbo.AssessmentDefinitionTest adt ON ad.Id = adt.Id
    JOIN 
        dbo.Course c ON ad.CourseId = c.Id
    JOIN 
        dbo.AssessmentMethod am ON ad.AssessmentTypeId = am.Id
    WHERE 
        ad.IsActive = 1
        AND ((ISNULL(@AssessmentName, '') = '') OR (ad.Name LIKE '%'+@AssessmentName+'%'))
        AND ((@CourseIdList IS NULL)
             OR (ad.CourseId IN (SELECT Id
                            FROM dbo.fnIntTableFromXML(@CourseIdList))))
        AND ((@AssessmentMethodIdList IS NULL)
             OR (ad.AssessmentTypeId IN (SELECT Id
                                         FROM dbo.fnIntTableFromXML(@AssessmentMethodIdList))))
        AND (@UserId = adt.OwnerUserId);
END;

存储

public async Task<List<AssessmentDTO>> SearchDefinition(AssessmentSearchDTO model)
{
    try
    {
        var courseIds = model?.Courses?.Select(x => x.Id)?.ToXMLString();
        var typeIds = model?.Types?.Select(x => x.Id)?.ToXMLString();
        var userId = _context.Authentication.User.Identity.GetUserId();
        var clientId = _context.Authentication.User.Identity.GetClientId();

        var searchResults = await Db.spAssessmentDefinitionTestSearch(model.Name, courseIds, typeIds, userId, clientId).ToListAsync();

        var result = searchResults.Select(x => new AssessmentDTO
        {
            Id = x.Id,
            Name = x.Name,
            Course = x.Course,
            Type = x.Method
        }).ToList();

        return result;
    }
    catch (Exception e)
    {
        throw e;
    }
}

HTML

    <div class="row">
    <div class="col-xs-4">
           <label>Name</label>
           <input ng-model="searchCriteria.Name" type="text" class="form-control" />
    </div>
    <div class="col-xs-4">
            <label>Course</label>
            <select ng-model="searchCriteria.Courses" id="" class="form-control" ng-options="type as type.Name for type in Courses"></select>
     </div>
      <div class="col-xs-4">
                 <label>Type</label>
                 <select ng-model="searchCriteria.Types" id="" class="form-control" ng-options="type as type.Name for type in Types"></select>
       </div>
    </div>
<div class="col-xs-4">
   <button class="btn btn-search full-width btn-full-width btn-primary" style="height:100%;" ng-click="searchDefinition(searchCriteria.Name, searchCriteria.Courses, searchCriteria.Types)" ><i class="fa fa-search"></i> Search</button>
</div>
        <!-- This uses a directive -->
    <bs-paginated-table list="assessments"
                        sortable="true"
                        default-display-num="20"
                        initial-predicate="'Course'"
                        show-display-select="true"
                        show-filter="true"
                        inline-edit="'none'"
                        columns="DefinitionColumns"
                        buttons="DefinitionButtons"
                        empty-results-message="'Well, it looks like there\'s no results to match those terms...'"></bs-paginated-table>

AngularCtrl

    $scope.searchCriteria = searchCriteria.data;
    $scope.searchDefinition = function (Name, Course, Type) {
        if (!$scope.searchCriteria) {
            $scope.searchCriteria = {};
        }
        toastr.info('Searching Assessments...', {
            timeOut: 3,
            extendedTimeout: 1
        });

        var newSearch = {
            Name: $scope.searchCriteria.Name,
            Courses: $scope.searchCriteria.Courses,
            Types: $scope.searchCriteria.Types
        };

        console.log(newSearch);
        assessmentControllerService.searchDefinition(newSearch)
            .then(
                function (data) {
                    console.log(data);
                    $scope.assessments = data.data;
                    if ($scope.searchCallback) {
                        $scope.searchCallback($scope.assessments);
                    }
                    $scope.loadingResults = false;
                },
                function (errCallback) {
                    bootbox.alert("An error has occurred.");
                })
    };

0 个答案:

没有答案