簡體   English   中英

ASP.net Core 2.1 中的 Knockout js 返回我未定義?

[英]Knockout js in ASP.net Core 2.1 returns me undefined?

我對 Kcnockout Js 有問題,當他嘗試返回結果時,他將結果無差別地返回給我,我已經驗證了承包商,如果他正在向我返回數據,如下圖所示,

在此處輸入圖片說明

但是當在視圖中加載值列表時,它告訴我它是未定義的,如下圖所示,

在此處輸入圖片說明

我不知道他為什么給我發這條消息,這是我的控制器代碼,

public JsonResult GetGender()
{
    ServiceResult serviceResult = new ServiceResult();

    serviceResult = this._genderServices.GetListGenders();

    return Json(serviceResult);

}

這是視圖代碼,

<h2>Generos</h2>
<hr />
<p data-bind="visible: IsNewButton">
    <a href="#" data-bind="click: New" class="btn btn-primary">Crear Nuevo</a>
</p>
<div id="divListGenders" data-bind="visible: ShowResult">
    <table class="table">
        <thead>
            <tr>
                <th>
                    Genero
                </th>
                <th></th>
                <th></th>
            </tr>
        </thead>
        <tbody data-bind="foreach: Genders">
            <tr>
                <td style="text-align:left" data-bind="text: GenderName"></td>
                <td style="text-align:center">
                    <a href="#" data-bind="click: $parent.Edit" class=" btn btn-warning"><span class="glyphicon glyphicon-edit"></span></a> |
                    <a href="#" data-bind="click: $parent.Details" class=" btn btn-info"><span class="glyphicon glyphicon-eye-open"></span></a> |
                    <a href="#" data-bind="click: $parent.Delete" class=" btn btn-danger"><i class="fas fa-trash"></i></a>
                </td>
            </tr>
        </tbody>
    </table>
</div>
<div data-bind="visible: IsBackToListButton">
    <a href="#" data-bind="click: BackToList" class="btn btn-info">Regresar</a>
</div>
<script src="~/js/Gender.js"></script>

這是返回流派列表的方法,

    var LoadListGenders = function () {
        $.ajax({
            url: '/AdminGenders/GetGender',
            data: null,
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            async: true,
            success: function (data) {
                if (data.Success) {
                    genderViewModel.Genders.removeAll();
                    genderViewModel.Genders(data.Data);
                }
                else {
                    alert(data.Data);
                }
            },
            error: function (ex) {
                alert('Ocurrión un error, cargando los generos.');
            }
        });

    };

}

我在 asp.net MVC 5 上提供了它並且它運行良好,我不知道為什么它在 ASP.NET Core 2.1 中對我不起作用,我很感激你的幫助

ASP.NET Core中,將輸出序列化為JSON的默認情況是駝峰式,而ASP.NET MVC 5則是 Pascal 情況。

請注意,您帖子中的第一張圖片顯示的所有屬性都是駝峰式的:

Object { success: true, message: null, data: Array(1) ... }

data數組中的項目類似: genderIdgenderName等。

解決此問題的一種方法是通過應用駝峰式屬性名稱更新您的javascript代碼和Knockout綁定。
例如

success: function (data) {
    if (data.success) {
        // ....
    }
    else {
        alert(data.data);
    }
}


或者,您可以選擇配置您的 Web API 以返回 Pacalcased JSON,例如通過在Startup ConfigureServices以下代碼:

services
    // ...
    .AddJsonOptions(options => {
        options.JsonSerializerOptions.PropertyNamingPolicy = null;
    })
    // ... 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM