ASP.Net MVC 4的的WebAPI不能正确绑定复选框? [英] ASP.Net MVC 4's WebAPI does not bind check boxes correctly?

查看:91
本文介绍了ASP.Net MVC 4的的WebAPI不能正确绑定复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我合并的WebAPI到我发展,我投递到的WebAPI控制器的所有表单提交。我注意到复选框没有得到绑定到模型正确。我使用有一个表格:

I'm incorporating WebAPI into my development and am Posting all form submissions to a WebAPI controller. I've noticed that checkboxes are not getting bound to the model correctly. I have a form using:

@Html.CheckBoxFor(m => m.HasVideo)

它生成一个复选框和一个隐藏的表单元素。当我选中该复选框(意思是真值),该模型在我的WebAPI后绑定反映了HasVideo属性假的。我把整个方法转移到传统的MVC控制器,并如期结合的作品。

It produces a checkbox and a hidden form element. When I check the checkbox (meaning a "true" value), the model binding in my WebAPI Post reflects a false for the HasVideo property. I moved the entire method over to a traditional mvc controller, and the binding works as expected.

对此有一个解决办法,或者是有,我失去了一些东西?

Is there a workaround for this, or is there something that I'm missing?

推荐答案

不使用的HTML帮助:

dont use this html helper :

@Html.CheckBoxFor(m => m.HasVideo)

试试这个来代替:

try this instead :

<input id="HasVideo" name="HasVideo" type="checkbox" value="true" @(((Model!=null) && Model.HasVideo) ? "checked=\"checked\"" : "" ) />

这篇关于ASP.Net MVC 4的的WebAPI不能正确绑定复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆