私は次のような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);
});
同じことを期待する別のアクションにDirectorySearchModel
、 model.Countries.arrCountries
常にnull
!arrCountries
MVCが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);
}
});