javascript - 模型未填充 JSON 数据

标签 javascript angularjs twitter-bootstrap angular-ui-grid

我是 Angular JS 的新手,并且在某个特定问题上停留了很长时间。

对于 UI 网格行,列值将在 linkcelltemplate 中。单击该按钮会打开一个 Bootstrap Modal 弹出窗口,其中具有案例快速搜索功能

这是一段代码:

<div ng-controller="CaseSearchCtrl">
    <div ng-show="case.togglePleaseWait">
        <div class="refresh" style="padding:15px;width:100%;height:100px;">
            <h4>Please wait....</h4>
        </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-heading text-center">
            Case Search
        </div>

        <table class="table table-striped">
            <tr>
                <td>
                    <select name="selSearchType" id="selSearchType" class="form-control" ng-model="case.searchTerm"
                            ng-init="case.searchTerm == 'caseNo'" ng-options="o as o for o in case.searchTerms"></select>
                </td>
                <td><input type="text" class="form-control" placeholder="enter search term" ng-model="case.input" /></td>
                <td><button class="btn btn-default" type="button" ng-click="case.quickSearch()">Quick Search</button></td>
            </tr>
        </table>    
    </div>
</div>

在输入文本框中,我们可以输入一个值并进行快速搜索。

enter image description here

这是我的 Controller 代码:

TransactionModule.controller("CaseSearchCtrl", ['$scope', '$uibModal', 'TransactionServices', 'CaseServices', 'CaseDataServices', 'TransactionDataServices', function ($scope, $uibModal, TransactionServices, CaseServices, CaseDataServices,TransactionDataServices) {

    /* Case implementation starts*/
    $scope.case = {

        searchTerm: "caseNo",    
        searchTerms: caseSearchTerms(),
        toggleQuickSearch: true,
        togglePleaseWait: false,
        name: "",
        number: "",
        type: "",
        constName: "",
        userName: "",
        status: "",
    }

    $scope.case.quickSearch = function () {
        $scope.case.togglePleaseWait = true;

        console.log($scope.case.input);

        if ($scope.case.searchTerm == "caseNo") {
            $scope.case.number = $scope.case.input;
        }
        else if ($scope.case.searchTerm == "caseName") {
            $scope.case.name = $scope.case.input;
        }
        else if ($scope.case.searchTerm == "constituentName") {
            $scope.case.constName = $scope.case.input;
        }
        else if ($scope.case.searchTerm == "userName") {
            $scope.case.userName = $scope.case.input;
        }
        else if ($scope.case.searchTerm == "status") {
            $scope.case.status = $scope.case.input;
        }
        else if ($scope.case.searchTerm == "type") {
            $scope.case.type = $scope.case.input;
        }
        var postParams = [
         {
            "CaseId": $scope.case.number,
            "CaseName": $scope.case.name,
            "ReferenceSource" : "",
            "ReferenceId" : "",
            "CaseType": $scope.case.type,
            "CaseStatus": $scope.case.status,
            "ConstituentName": $scope.case.constName,
            "UserName": $scope.case.userName,
            "ReportedDateFrom" : "",
             "ReportedDateTo" : "",
            "UserId": $scope.case.userName
        }]

        console.log("Post params are");

        console.log(postParams);


        CaseServices.getCaseAdvSearchResults(postParams).then(function (result) {
            if (result.length > 0) {
                $scope.case.togglePleaseWait = false;
                //constMultiDataService.setData(result, HOME_CONSTANTS.QUICK_CASE_SEARCH);
                console.log("Setting results for Quick search in TransactionDataServices");
                console.log(result);
             } ..........................

但是这里的 CaseInputSearchModel 在从 Controller 传递到 Web 服务时总是 NULL 。但是在控制台中我可以看到正在传递的列表。

getCaseAdvSearchResults: function (postCaseSearchParams) {
    console.log("Before sending to controller");
    console.log(postCaseSearchParams);
    return $http.post(BasePath + "CaseNative/AdvSearch", postCaseSearchParams, {
        //  return $http.post(BasePath + "Home/WriteCaseRecentSearches", postCaseSearchParams, {
        headers: {
            "Content-Type": "application/json",
            "Accept": "application/json"
        }
    }).then(function (result) {
        $http.post(BasePath + "Home/WriteCaseRecentSearches", postCaseSearchParams, {
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json"
            }
        });
        console.log(result);
        return result.data;
    });
},

enter image description here

即使参数已正确记录在控制台中。

enter image description here

这是我希望数据绑定(bind)到的模型的样子:

public class CaseInputSearchModel
{
    public string CaseId { get; set; }
    public string CaseName { get; set; }
    public string ReferenceSource { get; set; }
    public string ReferenceId { get; set; }
    public string CaseType { get; set; }
    public string CaseStatus { get; set; }
    public string ConstituentName { get; set; }
    public string UserName { get; set; }
    public string ReportedDateFrom { get; set; }
    public string ReportedDateTo { get; set; }
    public string UserId { get; set; }
}

最佳答案

如果添加 [FromBody]AdvSearch方法可以解决吗?此外,在 AdvSearch 中你有 ListCaseInputSearchModel作为类型。

AdvSearch([FromBody] CaseInputSearchModel model)

编辑: 没有注意到你正在发送一个数组。试试这个方法签名:

AdvSearch([FromBody] IEnuermable<CaseInputSearchModel> model)

关于javascript - 模型未填充 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36872028/

相关文章:

javascript - 如何在 ng-repeat 中显示 JSON object.array.object?

angularjs - 在我的 angularjs Controller 中获取 ng-change 字段的值

java - 使用 AngularJS 和 Spring 的路径变量

javascript - 尝试将 bootstrap-datepicker 放入我的 Rails 项目中

javascript - .show() 和 .toggle() 不会在问答游戏中显示最终游戏屏幕

javascript - 尝试在 Jade 中传递复选框值

javascript - 我应该在哪里添加 JS 事件才能使我的链接正常工作?

html - Bootstrap 表中标题下方的灰线

html - 启动 Bootstrap SB Admin 滚动导航栏元素

javascript - 如何始终显示 Bootstrap 3 模式?