use*_*429 6 asp.net-mvc asp.net-mvc-3 asp.net-mvc-2 knockout-mapping-plugin knockout.js
我有一个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; }
}
Run Code Online (Sandbox Code Playgroud)
CountriesCollection对象(第9行)如下所示:
public class CountriesCollection
{
[Display(Name = "Countries")]
public int[] arrCountries { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在,我正在创建一个新的,空白的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"}]
}
Run Code Online (Sandbox Code Playgroud)
我的复选框呈现为:<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"}]
}
Run Code Online (Sandbox Code Playgroud)
正常提交我的视图(即通过提交按钮,而不是Knockout.js)提交给期望的MVC动作DirectorySearchModel
,我可以要求model.Countries.arrCountries
获取已检查项目的列表,但是当我使用时......
$.post("/MyController/MyAction", ko.toJS(viewModel), function(returnData) {
$("#resultCount").html(returnData);
});
Run Code Online (Sandbox Code Playgroud)
要么...
$.post("/MyController/MyAction", viewModel, function(returnData) {
$("#resultCount").html(returnData);
});
Run Code Online (Sandbox Code Playgroud)
给需要同另一个动作DirectorySearchModel
, model.Countries.arrCountries
是永远null
!我想知道是否由于Knockout.js 在MVC期待s时将arrCountries
条目发布为string[]
s int[]
,但是将我的MVC代码更改为期望string[]
s似乎没有太大改变......!内部的CountriesCollection
对象DirectorySearchModel
似乎存在,但它arrCountries
始终存在于内部null
.
有任何想法吗?任何帮助非常感谢!
接收Knockout.js viewModel的操作:
public MvcHtmlString ResultCount(DirectorySearchModel model)
{
return new MvcHtmlString(getResultCount(model).ToString());
}
Run Code Online (Sandbox Code Playgroud)
该getResultCount
方法:
public int getResultCount(DirectorySearchModel model)
{
IUserRepository userRepository = new UserRepository();
int count = userRepository.Search(model, null).Count();
return count;
}
Run Code Online (Sandbox Code Playgroud)
感谢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);
}
});
Run Code Online (Sandbox Code Playgroud)
您不能使用 $.post 您需要寻找底层 $.ajax 并添加正确的 contenttype 以使 mvc 接受发布的 json 并执行模型绑定(contenttype 应该是“application/json; charset=utf-8”)google对于它,你会看到很多例子
归档时间: |
|
查看次数: |
2489 次 |
最近记录: |