>我有一个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; }
}
国家/地区集合对象(第 9 行(如下所示:
public class CountriesCollection
{
[Display(Name = "Countries")]
public int[] arrCountries { get; set; }
}
现在,我正在创建一个新的,空白的CountryCollection实例,然后将其添加到DirectorySearchModel视图模型的空白实例中,然后将其全部序列化为Knockout的javascript视图模型.js:
{
"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(到期望DirectorySearchModel
的 MVC 操作,我可以要求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
!我想知道这是否是由于 Knockout.js当 MVC 期望 int[]
s 时将arrCountries
条目发布为 string[]
s,但更改我的 MVC 代码以期望 string[]
s 似乎没有太大变化..!DirectorySearchModel
中的CountriesCollection
对象似乎存在,但它始终null
arrCountries
。
有什么想法吗?任何帮助非常感谢!
编辑
接收挖空.js视图模型的操作:
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;
}
固定!
感谢 Konstantin 指出,只需从 $.post 切换到 $.ajax 即可将我的 Knockout.js 视图模型发送回我的 mvc 操作!这是我正在使用的 $.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);
}
});
你不能使用 $.post,你需要去底层的 $.ajax 并添加正确的内容类型,使 MVC 接受发布的 JSON 并进行模型绑定(内容类型应该是"application/json;charset=utf-8"( 谷歌搜索它,你会 se 很多例子