Asp.net mvc 4 如何在MVC4中保存所选单选按钮值
我在Asp.net mvc 4 如何在MVC4中保存所选单选按钮值,asp.net-mvc-4,Asp.net Mvc 4,我在MVC4中工作。在这里,我将以下代码用于单选按钮: 型号: public class PlatInspHistoryModels { public List<RadioButtonItem> RadioButtonList { get; set; } public string SelectedRadioButton { get; set; } } public class RadioButtonItem { public string Name { get
MVC4
中工作。在这里,我将以下代码用于单选按钮
:
型号:
public class PlatInspHistoryModels
{
public List<RadioButtonItem> RadioButtonList { get; set; }
public string SelectedRadioButton { get; set; }
}
public class RadioButtonItem
{
public string Name { get; set; }
public bool Selected { get; set; }
public string Value { get; set; }
public bool Visible { get; set; }
}
public ActionResult Index()
{
var viewModel = new PlatInspHistoryModels
{
RadioButtonList = new List<RadioButtonItem>
{
new RadioButtonItem
{
Name = "Topside", Value = "T",Selected = true,Visible = true
},
new RadioButtonItem
{
Name="Underwater", Value = "U",Selected = false,Visible = true
}
}
};
return View(viewModel);
}
@using (Html.BeginForm("Index", "PlatInspHistory", FormMethod.Post, new { id = "form" }))
{
<table cellpadding="4" cellspacing="4">
<tr>
<td>
foreach (Cairs2.Models.RadioButtonItem item in Model.RadioButtonList)
{
@Html.DisplayFor(i => item.Name)
@Html.RadioButton("PlatInspHistoryModels.SelectedRadioButton", item.Value, item.Selected, new { @class = "formCheckbox", tabindex = "1" })
}
</td>
</tr>
</table>
}
将您的
foreach
循环替换为for
循环,并使用强类型帮助程序:
for (var i = 0; i < Model.RadioButtonList.Count; i++)
{
@Html.DisplayFor(x => x.RadioButtonList[i].Name)
@Html.HiddenFor(x => x.RadioButtonList[i].Name)
@Html.RadioButtonFor(x => x.SelectedRadioButton, Model.RadioButtonList[i].Value, new { @class = "formCheckbox", tabindex = "1" })
}
for(变量i=0;ix.RadioButtonList[i].Name)
@Html.HiddenFor(x=>x.RadioButtonList[i].Name)
@Html.RadioButton(x=>x.SelectedRadioButton,Model.RadioButtonList[i]。值,新{@class=“formCheckbox”,tabindex=“1”})
}
我在MVC5工作,我有一些单选按钮形式的MCQ问题,这些单选按钮是从数据库中随机获取的。这是我的“[表(“MCQ”)]
和控制器
public ActionResult Index()
{ return View((from a in ne.Mcqs orderby Guid.NewGuid() select a).Take(2).ToList()); }
和视图
@foreach (var item in Model)
{<tr>
<td>
@Html.DisplayFor(modelItem=>item.Qid)</td>
<td> @Html.DisplayFor(modelItem => item.Question)</td> <td>
@Html.RadioButton("skillsq1", new { id = "1"}).
@Html.DisplayFor(modelItem => item.AnsOpt1)</td><td>
@Html.RadioButton("skillsq1", new { id = "2"})
@Html.DisplayFor(modelItem => item.AnsOpt2) </td><td>
@Html.RadioButton("skillsq1", new { id = "3" })
@Html.DisplayFor(modelItem => item.AnsOpt3) </td> <td>
@Html.RadioButton("skillsq1", new { id = "4" })
@Html.DisplayFor(modelItem => item.AnsOpt4) </td> </tr>}
@foreach(模型中的变量项)
{
@DisplayFor(modeleItem=>item.Qid)
@DisplayFor(modelItem=>item.Question)
@RadioButton(“skillsq1”,new{id=“1”})。
@DisplayFor(modeleItem=>item.AnsOpt1)
@RadioButton(“skillsq1”,新的{id=“2”})
@DisplayFor(modeleItem=>item.AnsOpt2)
@RadioButton(“skillsq1”,新的{id=“3”})
@DisplayFor(modeleItem=>item.AnsOpt3)
@RadioButton(“skillsq1”,新的{id=“4”})
@DisplayFor(modeleItem=>item.AnsOpt4)}
我想问的是,我如何在您的
ActionResult Index
中获得洗牌问题的选中单选按钮的值,您将只收到选定的收音机,而不是列表。哦,我现在已经删除了第二个表单。但是@Darin我如何在保存时获得选定的值?for循环在这里有什么用?对不起,我犯了一个错误。单选按钮应绑定到模型上的SelectedRadioButton
属性。我已经更新了我的答案。
public ActionResult Index()
{ return View((from a in ne.Mcqs orderby Guid.NewGuid() select a).Take(2).ToList()); }
@foreach (var item in Model)
{<tr>
<td>
@Html.DisplayFor(modelItem=>item.Qid)</td>
<td> @Html.DisplayFor(modelItem => item.Question)</td> <td>
@Html.RadioButton("skillsq1", new { id = "1"}).
@Html.DisplayFor(modelItem => item.AnsOpt1)</td><td>
@Html.RadioButton("skillsq1", new { id = "2"})
@Html.DisplayFor(modelItem => item.AnsOpt2) </td><td>
@Html.RadioButton("skillsq1", new { id = "3" })
@Html.DisplayFor(modelItem => item.AnsOpt3) </td> <td>
@Html.RadioButton("skillsq1", new { id = "4" })
@Html.DisplayFor(modelItem => item.AnsOpt4) </td> </tr>}