下面是我视图中显示的一组单选按钮。我可以通过这个简单的代码检索所选项目
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 </label>
</form>
答案 0 :(得分: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)
在回发后,所选单选按钮保持选中状态。
答案 1 :(得分:1)
如果我理解您的需要,您可以在ViewBag(或ViewData或Model)中设置要检查的项目的名称,并相应地在视图中设置checked属性。像这样:
<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 </label>
</form>
答案 2 :(得分: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
}
});