asp.net-mvc – Knockout.js&复选框列表:发布到mvc控制器
我有一个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"},"CountryId":3,"CountryName":"Spain"}],"AllCountries":[{"RelationshipManager":{},"CountryName":"Spain"}] } 我的复选框呈现为:< input checked =“checked”data-bind =“checked:Countries.arrCountries”id =“Countries_arrCountries30”name =“Countries.arrCountries”type =“checkbox”value =“1”>.检查一对夫妇意味着你最终得到这个Knockout.js视图模型: { "FirstName":null,"Countries":{"arrCountries":["1","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对象似乎存在,但是其中的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; } 固定! 感谢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并添加正确的contenttype以使mvc接受发布的json并进行模型绑定(contenttype应为“application / json; charset = utf-8”)google对于它,你会有很多例子
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-3 – IoC对泛型的关注点分离
- ABP官方文档(四十)【ASP.NET Core】
- asp.net-mvc – 返回空ActionResult
- asp.net-mvc – Razor ViewEngine HTML.Checkbox方法创建一
- asp.net – IIS 7 URL重写导致Google AdWords / Analytics跟
- asp.net – 如何MSDeploy构建的网站包到一个处女IIS网站
- ASP.NET Core 3.1 WebApi+JWT+Swagger+EntityFrameworkCore
- asp.net-mvc – 从DataAnnotation类中渲染LabelFor无标签标
- ef-code-first – 如何首先使用代码向Identity默认表AspNet
- 在ASP.NET 4 ReportViewer中使用对象数据源
- asp.net – 外键在VS2012 RC中未被识别
- asp.net – 我应该使用Orchard或umbraco还是别的
- asp.net-core – 在.NET Core Web API上为CORS启
- asp.net-mvc-4 – 为什么我们需要MVC中的Web API
- asp.net-mvc – 我可以从HttpContext获取控制器吗
- 学习:正则表达式的基本语法
- 使用asp.net核心身份将数据存储在cookie中
- ASP.NET使用百度eCharts
- asp.net – 带有requireSSL for cookies的Siteco
- ASP.NET 2.5前缀ctl00和ASP.NET 4不是前缀ctl00