2

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

string criteria = filter["criteria"];

但是我不知道如何保留所选项目。一旦控制器回发到视图,默认单选按钮总是被选中。

    <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>
4

3 回答 3

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>

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

public ActionResult Index(string criteria, string searchValue)

回发后选中的单选按钮保持选中状态。

于 2013-04-18T17:01:04.493 回答
1

如果我正确理解您的需求,您可以在 ViewBag(或 ViewData 或 Model)中设置要检查的项目的名称,并相应地在视图中设置选中的属性。像这样的东西:

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

        @{
            var checkedItemName = ViewBag.CheckedItemName;
        }
        <input type="radio" name="criteria" id="bankName" value="bankName" checked="@((checkedItemName == "bankName"))"/>
        <label for="bankName">Bank Name</label>
        <input type="radio" name="criteria" id="EPURL" value="EPURL" checked="@((checkedItemName == "EPURL"))"/>
        <label for="EPURL">EPURL</label>
        <input type="radio" name="criteria" id="specialNotes" value="specialNotes" checked="@((checkedItemName == "specialNotes"))"/>
        <label for="SpecialNotes">Special Notes</label>
        <input type="radio" name="criteria" id="email" value="email" checked="@((checkedItemName == "email"))"/>
        <label for="email">Email</label>
        <input type="radio" name="criteria" id="dynamicsId" value="dynamicsId" checked="@((checkedItemName == "dynamicsId"))"/>
        <label for="dynamicsId">Dynamics ID</label>
        <input type="radio" name="criteria" id="stat" value="stat" checked="@((checkedItemName == "stat"))"/>
        <label for="fixed">Agent ID &nbsp;</label>
    </form>
于 2013-04-17T21:49:46.690 回答
1

不要刷新整个页面...使用 JQuery 并进行 Ajax 调用

$.ajax({
    url: "Your Url",
    async: true,
    type: 'POST',
    data: JSON.stringify({ ParameterName: Param_Value }),
    beforeSend: function (xhr, opts) {

    },
    contentType: 'application/json; charset=utf-8',
    complete: function () {  },
    success: function (data) {
        //Success Callback
        //Code to set value to your control
    }
});
于 2013-04-18T06:26:19.567 回答