首先,我必须在mvc3局部视图中创建一些单选按钮。
当我在屏幕上时,我只需要选择一个单选按钮并检索特定值。
我该怎么做(例如JS或C#)?
<div id="UserDeleteInfosField">
<p>
@Html.RadioButton("HasUserLeave", new { id = "rb1" })
@UserAccountResources.UserLeave
@Html.HiddenFor(x => x.UserLeave)
</p>
<br />
<p>
@Html.RadioButton("HasUserTransfer", new { id = "rb2" })
@UserAccountResources.UserTransfer
@Html.HiddenFor(x => x.UserTransfer)
</p>
<br />
<p>
@Html.RadioButton("HasUserDetachment", new { id = "rb3" })
@UserAccountResources.UserDetachment
@Html.HiddenFor(x => x.UserDetachment)
</p>
<br />
<p>
@Html.RadioButton("HasUserRetirement", new { id = "rb4" })
@UserAccountResources.UserRetirement
@Html.HiddenFor(x => x.UserRetirement)
</p>
<br />
<p>
@Html.RadioButton("HasUserStatus", new { id = "rb5" })
@UserAccountResources.UserStatus
@Html.HiddenFor(x => x.UserStatus)
</p>
</div>
提前致谢!
Ars_n
答案 0 :(得分:8)
所以你需要一组radiobuttons?我使用自定义助手。
帮助者代码:
public static MvcHtmlString RadioButtonForSelectList<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<SelectListItem> listOfValues)
{
var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var sb = new StringBuilder();
if (listOfValues != null)
{
// Create a radio button for each item in the list
foreach (SelectListItem item in listOfValues)
{
// Generate an id to be given to the radio button field
var id = string.Format("{0}_{1}", metaData.PropertyName, item.Value);
// Create and populate a radio button using the existing html helpers
var label = htmlHelper.Label(id, HttpUtility.HtmlEncode(item.Text));
var radio = htmlHelper.RadioButtonFor(expression, item.Value, new { id = id }).ToHtmlString();
if (item.Selected)
{
radio = htmlHelper.RadioButtonFor(expression, item.Value, new { id = id, @checked = "checked", }).ToHtmlString();
}
// Create the html string that will be returned to the client
// e.g. <input data-val="true" data-val-required="You must select an option" id="TestRadio_1" name="TestRadio" type="radio" value="1" /><label for="TestRadio_1">Line1</label>
sb.AppendFormat("<div class=\"RadioButton\">{0}{1}</div>", radio, label);
}
}
return MvcHtmlString.Create(sb.ToString());
}
现在在您的视图中,您可以使用:
@Html.RadioButtonForSelectList(model => model.yourproperty, Model.listUsedToPopulate)
现在你只能检查其中一个无线电按钮。选中的值存储在model.yourproperty中。
答案 1 :(得分:5)
查看示例以了解单选按钮的分组方式:example
您需要相同的name
属性,例如:
<input type="radio" name="group2" value="Water" /> Water<br />
<input type="radio" name="group2" value="Beer" /> Beer<br />
<input type="radio" name="group2" value="Wine" checked /> Wine<br />
你的例子:
@Html.RadioButton("SameGroup", "rb1")
@Html.RadioButton("SameGroup", "rb2")
@Html.RadioButton("SameGroup", "rb3")
@Html.RadioButton("SameGroup", "rb4")
@Html.RadioButton("SameGroup", "rb5")
仅在帖子的控制器操作中收到的检查值。
答案 2 :(得分:1)
我猜你想把它发布到一个控制器方法,所以也许你正在尝试这样做:
假设控制器方法采用名为myParameter
的参数,那么您需要将单选按钮的名称设置为相同以对它们进行分组,然后将值设置为您需要的值:
<div id="UserDeleteInfosField">
<p>
@Html.RadioButton("myParameter", UserAccountResources.UserLeave)
@UserAccountResources.UserLeave
@Html.HiddenFor(x => x.UserLeave)
</p>
<br />
<p>
@Html.RadioButton("myParameter", UserAccountResources.UserTransfer)
@UserAccountResources.UserTransfer
@Html.HiddenFor(x => x.UserTransfer)
</p>
<br />
<p>
@Html.RadioButton("myParameter", UserAccountResources.UserDetachment)
@UserAccountResources.UserDetachment
@Html.HiddenFor(x => x.UserDetachment)
</p>
<br />
<p>
@Html.RadioButton("myParameter", UserAccountResources.UserRetirement)
@UserAccountResources.UserRetirement
@Html.HiddenFor(x => x.UserRetirement)
</p>
<br />
<p>
@Html.RadioButton("myParameter", UserAccountResources.UserStatus)
@UserAccountResources.UserStatus
@Html.HiddenFor(x => x.UserStatus)
</p>
</div>
答案 3 :(得分:0)
感谢您的回答。
我最终为每个单选按钮创建了一个组。 我在rb对象参数中调用该属性。
<div id="UserDeleteInfosField">
<p>
@Html.RadioButton("RbDeleteGroup", Model.UserLeave)
@UserAccountResources.UserLeave
@Html.HiddenFor(x => x.UserLeave)
</p>
<br />
<p>
@Html.RadioButton("RbDeleteGroup", Model.UserTransfer)
@UserAccountResources.UserTransfer
@Html.HiddenFor(x => x.UserTransfer)
</p>
<br />
<p>
@Html.RadioButton("RbDeleteGroup", Model.UserDetachment)
@UserAccountResources.UserDetachment
@Html.HiddenFor(x => x.UserDetachment)
</p>
<br />
<p>
@Html.RadioButton("RbDeleteGroup", Model.UserRetirement)
@UserAccountResources.UserRetirement
@Html.HiddenFor(x => x.UserRetirement)
</p>
<br />
<p>
@Html.RadioButton("RbDeleteGroup", Model.UserStatus)
@UserAccountResources.UserStatus
@Html.HiddenFor(x => x.UserStatus)
</p>
</div>
所以在这种情况下,我只能同时检查一个单选按钮,并从相应的ViewModel中检索属性值。
[Display(Name = "UserAccountDeleteInfos", ResourceType = typeof(UserAccountResources))]
public string UserLeave
{
get { return UserAccountResources.UserLeave; }
}