簡體   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