asp.net-mvc 如何在 mvc4 中使用单选按钮列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24101330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use radiobuttonlist in mvc4
提问by rumi
I need to create a simple radio button list selection to choose from the following values:
我需要创建一个简单的单选按钮列表选择以从以下值中进行选择:
- Agree
- Disagree
- Not sure
- 同意
- 不同意
- 没有把握
How would we add it in the following razor view? Is it better to create an enum of these values in the QuestionModeland use foreachto bind with html helper.
我们将如何在以下剃刀视图中添加它?在 中创建这些值的枚举QuestionModel并用于foreach与 html helper 绑定是否更好。
Any example or ideas?
任何例子或想法?
@model Survey.Models.Question
@using (Html.BeginForm("Index", "Home", new { QuestionId = Model.QuestionId }))
{
<h2>Survey</h2>
<fieldset>
<legend>Please Choose</legend>
<p>
Question ID:
@Model.QuestionId
</p>
<p>
Description:
@Model.Description
</p>
<input type="submit" value="Next" id="submitButton" />
</fieldset>
}
回答by Jonathan Walton
Using the ASP.NET Html Helpers, there are three various methods you can use for each of the different helpers supplied for radio buttons.
使用 ASP.NET Html 帮助程序,您可以对为单选按钮提供的每个不同帮助程序使用三种不同的方法。
Method 1
方法一
A simple way to make a radio button list:
制作单选按钮列表的简单方法:
<div>
<label>
@Html.RadioButton("Answer", "Agree")
Agree
</label>
</div>
<div>
<label>
@Html.RadioButton("Answer", "Disagree")
Disagree
</label>
</div>
<div>
<label>
@Html.RadioButton("Answer", "Not Sure")
Not Sure
</label>
</div>
Method 2
方法二
The above code will post the checked value with a name of "Answer" to the server when you click Submit. If you would like to make this strongly typed, as you did with Model.QuestionId and Model.Description, you could add a Answer property to your model and do the following:
当您单击提交时,上面的代码会将选中的值以“Answer”的名称发布到服务器。如果你想让这个强类型,就像你对 Model.QuestionId 和 Model.Description 所做的那样,你可以向你的模型添加一个 Answer 属性并执行以下操作:
<div>
<label>
@Html.RadioButtonFor(m => m.Answer, "Agree")
Agree
</label>
</div>
<div>
<label>
@Html.RadioButtonFor(m => m.Answer, "Disagree")
Disagree
</label>
</div>
<div>
<label>
@Html.RadioButtonFor(m => m.Answer, "Not Sure")
Not Sure
</label>
</div>
Method 3
方法三
Finally, if you would like to get fancy, you can use an enum. Place the following in your controller:
最后,如果您想花哨的话,可以使用枚举。将以下内容放入您的控制器中:
public enum AnswerType {
Agree,
Disagree,
NotSure
}
Next, add the property to your model:
接下来,将属性添加到您的模型中:
public AnswerType AnswerType { get; set; }
And then add the following to your view:
然后将以下内容添加到您的视图中:
@Html.RadioButtonForEnum(m => m.AnswerType)
Hope this helps!
希望这可以帮助!
回答by Friyank
this will work Radio button for model
这将起作用 模型的单选按钮
@using (Html.BeginForm())
{
foreach (var department in Model.Departments)
{
@Html.RadioButtonFor(m => m.SelectedDepartment, department.Id) @department.Name
}
<input type="submit" value="Submit" />
}
回答by wshipley
In your viewmodel you could have a public int SelectedRating. You could make it a string and change the values from 114... to string values as well.
在您的视图模型中,您可以有一个 public int SelectedRating。您可以将其设为字符串并将值从 114... 更改为字符串值。
<div class="radio">
<label>
@Html.RadioButtonFor(x => x.SelectedRating, 114)
<span>Not Sure</span>
</label>
</div>
<div class="radio">
<label>
@Html.RadioButtonFor(x => x.SelectedRating, 115)
<span>Agree</span>
</label>
</div>
<div class="radio">
<label>
@Html.RadioButtonFor(x => x.SelectedRating, 116)
<span>Don't Agree</span>
</label>
</div>

