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

Pho*_*_uy 7 html c# asp.net-mvc razor asp.net-mvc-4

我有一个剃刀视图(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 />
        }
Run Code Online (Sandbox Code Playgroud)

location.ThirdValue是布尔属性,对该属性进行调试它很好..但在HTML中我得到value="value"而不是value="false".

发生了什么?

Wjd*_*is5 8

看到这个答案

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


Ale*_*dov 1

做这个

<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)" />
Run Code Online (Sandbox Code Playgroud)

该值不是设置复选框是否选中,值有不同的功能。例如,如果您将值设置为“test”并选中复选框,则当您提交表单时,您将看到提交的变量的真实值将是“test”;

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