从ASP MVC 3控制器设置单选按钮检查项目

Nea*_*alR 2 asp.net-mvc postback radio-button asp.net-mvc-3 asp.net-mvc-2

下面是我视图中显示的一组单选按钮.我可以通过这个简单的代码检索所选项目

string criteria = filter["criteria"];
Run Code Online (Sandbox Code Playgroud)

但是我不知道如何保留所选项目.控制器回发到视图后,将始终选择默认单选按钮.

    <form method="post">
        @Html.TextBox("searchValue", ViewBag.CurrentFilter as string, new { placeholder = "Search" })
        <input type="image" src="@Url.Content("~/Content/Images/Filter.bmp")" alt="Filter" style="padding-top: 0px;" />
            <span class="error" style="clear: both;">
                @ViewBag.ErrorMessage
           </span>
        <a href="#" style="padding-left: 30px;"></a>
        <br />
        <br />
        <input type="radio" name="criteria" id="bankName" value="bankName" checked="true"/>
        <label for="bankName">Bank Name</label>
        <input type="radio" name="criteria" id="EPURL" value="EPURL" />
        <label for="EPURL">EPURL</label>
        <input type="radio" name="criteria" id="specialNotes" value="specialNotes" />
        <label for="SpecialNotes">Special Notes</label>
        <input type="radio" name="criteria" id="email" value="email" />
        <label for="email">Email</label>
        <input type="radio" name="criteria" id="dynamicsId" value="dynamicsId" />
        <label for="dynamicsId">Dynamics ID</label>
        <input type="radio" name="criteria" id="stat" value="stat" />
        <label for="fixed">Agent ID &nbsp;</label>
    </form>
Run Code Online (Sandbox Code Playgroud)

Nea*_*alR 5

这个问题的答案非常简单.我犯的第一个错误是没有使用@Html控件.其次是FormCollection用作索引控制器的输入参数.通过将单选按钮更改为以下内容:

        @Html.RadioButton("criteria", "bankName", true)<span>Bank Name</span>
        @Html.RadioButton("criteria", "EPURL")<span>EPURL</span>
        @Html.RadioButton("criteria", "specialNotes")<span>Special Notes</span>
        @Html.RadioButton("criteria", "email")<span>Email</span>
        @Html.RadioButton("criteria", "dynamicsId")<span>Dynamics ID</span>
        @Html.RadioButton("criteria", "stat")<span>Agent ID</span>
Run Code Online (Sandbox Code Playgroud)

以及Index控制器中方法的签名:

public ActionResult Index(string criteria, string searchValue)
Run Code Online (Sandbox Code Playgroud)

在回发后,所选单选按钮保持选中状态.