繁体   English   中英

MVC 5 Razor视图没有绑定bool输入

[英]MVC 5 Razor view not binding bool to input

我有一个剃刀视图(Framework 4.5,MVC 5)和一个html输入类型=复选框,其值等于模型布尔值,但它不是true或false,而是绑定“value”。

这是我的代码:

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是boolean属性,对该属性进行调试它很好..但在HTML中我得到value="value"而不是value="false"

发生了什么?

看到这个答案

基本上你想使用HTML.CheckBoxFor(model => model.booleanProperty)

做这个

<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)" />

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

你可以用它做很酷的东西。 例如,表单上有3个复选框。 它们都有相同的名称,但值不同。 当您提交表单时,您获得的结果将是逗号分隔的字符串,其中包含已选中复选框的值;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM