首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MVC 5 Razor视图未将bool绑定到输入

MVC 5 Razor视图未将bool绑定到输入
EN

Stack Overflow用户
提问于 2013-11-26 02:56:13
回答 2查看 5.5K关注 0票数 7

我有一个剃刀视图(Framework4.5,MVC5)和一个html输入type=checkbox,它的值等于模型布尔值,但它绑定的不是真或假,而是“值”。

这是我的代码:

代码语言:javascript
运行
复制
for (int index = 0; index < Model.Item1.Locations.Length; index++)
        {
            WSTupleOfInt32StringStringBoolean location = Model.Item1.Locations[index];
                <div class="editor-field">
                    <input id="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].Key", index, Model.Item2)" type="checkbox" value="@location.ThirdValue" name="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].ThirdValue", index, Model.Item2)" />
                </div>
                <div class="editor-label">
                    <label for="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].Key", index, Model.Item2)">@location.FirstValue</label>
                    @if (!string.IsNullOrEmpty(location.SecondValue))
                    {
                        <a href="@string.Format("//maps.google.com/?q={0}", location.SecondValue)" target="_blank"><img alt="@location.SecondValue" src="@string.Format("//maps.google.com/maps/api/staticmap?center={0}&markers=color:blue|{0}&zoom=15&size=64x64&sensor=false", location.SecondValue)" /></a>
                    }
                </div><br />
        }

location.ThirdValue是布尔型属性,调试该属性就可以了。但是在超文本标记语言中,我得到的是value="value"而不是value="false"

发生什么事了呢?

EN

回答 2

Stack Overflow用户

发布于 2013-11-26 03:06:09

查看此Answer

基本上,您希望使用HTML.CheckBoxFor(model => model.booleanProperty)

票数 9
EN

Stack Overflow用户

发布于 2013-11-26 03:00:27

执行此操作

代码语言:javascript
运行
复制
<input id="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].Key", index, Model.Item2)" type="checkbox" @(location.ThirdValue ? "checked" : "") name="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].ThirdValue", index, Model.Item2)" />

值未设置复选框是否选中,值具有不同的功能。例如,如果您将value设置为'test‘并选中该复选框,当您提交表单时,您将看到提交变量的真值为’test‘而不是true;

你可以用它做一些很酷的事情。例如,您的表单上有3个复选框。它们都具有相同的名称,但值不同。当您提交表单时,您得到的结果将是逗号分隔的字符串,其值为选中的复选框;

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20200967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档