简体   繁体   English

不正确的单选按钮设置为“已选中”

[英]incorrect radio button set as "checked"

Newbie question, please be kind.新手问题,请善待。

I have this Index.cshtml page in C# MVC application.我在 C# MVC 应用程序中有这个 Index.cshtml 页面。 When the page is rendered, the last button is checked.当页面被渲染时,最后一个按钮被选中。 How do I make the first button (All) set to checked instead?如何将第一个按钮(全部)设置为选中?

I am reading thru the "similar questions", but none seem to address my question.我正在阅读“类似问题”,但似乎没有一个能解决我的问题。 Please help.请帮忙。 Thank you in advance if you can explain this to me.如果您能向我解释一下,请提前感谢您。

<h1>@ViewBag.title</h1>

<form action="/list/index" method="post">
    <h2>Search by:</h2>

    <p>
        @foreach (var column in ViewBag.columns)
        {
            <span>
                <input type="radio" name="searchType" id="@column.Key" value="@column.Key" checked="@column.Key == 'all'" />
                <label for="@column.Key">@column.Value</label>
            </span>
        }
    </p>

    <p>
        <label for="searchTerm">Keyword:</label>
        <input type="text" name="searchTerm" id="searchTerm" />
    </p>

    <input type="submit" value="Search" />
</form>

<hr />

@if (ViewBag.service != null)
{
    <table>
        @foreach (var srv in ViewBag.service)
        {
            <tr>
                <td>
                    <p>Provider: @srv.Provider.Name</p>
                    <p>Location: @srv.Location.Address</p>
                    <p>Category: @srv.Category.Name</p>
              @*      <p>Tag: @service.Tag</p>
                    <p>Keyword: @service.Keyword</p>*@
                </td>
            </tr>
        }
    </table>
}

In one condition:在一种情况下:

<input type="radio" name="searchType" id="@column.Key" value="@column.Key" @(column.Key == 'all' ? "checked" : " ") />

Checked or not checked?检查还是不检查?

You can just use checked for checked any radio button:您可以只使用checked的任何单选按钮:

<input type="radio" checked />

For conditionally check any of your radio button, use this:要有条件地检查您的任何单选按钮,请使用:

@if(column.Key == 'all'){
 <input type="radio" name="searchType" id="@column.Key" value="@column.Key" checked />
}else{
 <input type="radio" name="searchType" id="@column.Key" value="@column.Key" />
}

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

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