6

私は次のようなMVCビューモデルを持っています:

public class DirectorySearchModel
{
    [Display(Name = "First name contains")]
    public string FirstName { get; set; }

    [Display(Name = "Last name contains")]
    public string LastName { get; set; }

    public CountriesCollection Countries { get; set; }
    public IEnumerable<Country> SelectedCountries { get; set; }
    public IEnumerable<Country> AllCountries { get; set; }
}

CountriesCollectionオブジェクト(9行目)は次のようになります。

public class CountriesCollection
{
    [Display(Name = "Countries")]
    public int[] arrCountries { get; set; }
}

ここで、CountriesCollectionの新しい空白のインスタンスを作成し、それをDirectorySearchModelビューモデルの空白のインスタンスに追加して、すべてをKnockout.jsのjavascriptビューモデルにシリアル化します。

{
"FirstName":null,

"LastName":null,

"Countries":{"arrCountries":[]},

"SelectedCountries":[{"RelationshipManager":{},"CountryId":1,"CountryName":"UK"},{"RelationshipManager":{},"CountryId":2,"CountryName":"France"},{"RelationshipManager":{},"CountryId":3,"CountryName":"Spain"}],

"AllCountries":[{"RelationshipManager":{},"CountryId":1,"CountryName":"UK"},{"RelationshipManager":{},"CountryId":2,"CountryName":"France"},{"RelationshipManager":{},"CountryId":3,"CountryName":"Spain"}]
}

チェックボックスは次のように表示されます<input checked="checked" data-bind="checked: Countries.arrCountries" id="Countries_arrCountries30" name="Countries.arrCountries" type="checkbox" value="1">。カップルをチェックするということは、このKnockout.jsビューモデルになってしまうことを意味します。

{
"FirstName":null,

"LastName":null,

"Countries":{"arrCountries":["1", "3"]},

"SelectedCountries":[{"RelationshipManager":{},"CountryId":1,"CountryName":"UK"},{"RelationshipManager":{},"CountryId":2,"CountryName":"France"},{"RelationshipManager":{},"CountryId":3,"CountryName":"Spain"}],

"AllCountries":[{"RelationshipManager":{},"CountryId":1,"CountryName":"UK"},{"RelationshipManager":{},"CountryId":2,"CountryName":"France"},{"RelationshipManager":{},"CountryId":3,"CountryName":"Spain"}]
}

ビューを通常どおりに(つまり、Knockout.jsではなく送信ボタンを介して)MVCアクションに送信すると、チェックされたアイテムのリストを取得するDirectorySearchModelように要求できmodel.Countries.arrCountriesますが、...

$.post("/MyController/MyAction", ko.toJS(viewModel), function(returnData) {
    $("#resultCount").html(returnData);
});

また...

$.post("/MyController/MyAction", viewModel, function(returnData) {
    $("#resultCount").html(returnData);
});

同じことを期待する別のアクションにDirectorySearchModelmodel.Countries.arrCountries常にnullarrCountriesMVCがsを期待しているときにKnockout.jsがエントリをstring[]sとして投稿したためかどうか疑問に思いましint[]たが、MVCコードをstring[]sを期待するように変更してもそれほど変わらないようです。内のCountriesCollectionオブジェクトはDirectorySearchModel存在しているように見えますが、それはarrCountries常に内にありますnull

何か案は?どんな助けでも大歓迎です!

編集

Knockout.js viewModelを受け取るアクション:

public MvcHtmlString ResultCount(DirectorySearchModel model)
{
    return new MvcHtmlString(getResultCount(model).ToString());
}

getResultCount方法:

public int getResultCount(DirectorySearchModel model)
{
    IUserRepository userRepository = new UserRepository();
    int count = userRepository.Search(model, null).Count();
    return count;
}

修繕!

Knockout.jsビューモデルをmvcアクションに戻すために$.postから$.ajaxに切り替えるだけでよいことを指摘してくれた、Konstantinに感謝します。これが私が使用している$.ajaxコードです:

$.ajax({  
    type: "POST",
    url: "/MyController/MyAction",
    data: ko.toJSON(viewModel),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        $("#resultCount").html(data);
    }
});
4

1 に答える 1

2

$ .postを使用して、基になる$ .ajaxに移動し、正しいcontenttypeを追加して、mvcが投稿されたjsonを受け入れ、モデルバインディングを実行することはできません(contenttypeは "application / json; charset = utf-8"である必要があります)googleそれのために、あなたはたくさんの例を見るでしょう

于 2012-05-23T21:30:00.797 に答える