【发布时间】:2018-08-19 17:45:40
【问题描述】:
我正在开发一个动态多选表单生成器。在这种情况下,表单可以有多个带有不同问题和选项的单选按钮列表。
<strong>Question 1 </strong>
<br/>
<input type="radio" value=""><lable>Option1</lable>
<input type="radio" value=""><lable>Option2</lable>
<input type="radio" value=""><lable>Option3</lable>
<input type="radio" value=""><lable>Option4</lable>
<br />
<strong>Question 2 </strong>
<br />
<input type="radio" value=""><lable>Option5</lable>
<input type="radio" value=""><lable>Option6</lable>
我为此制作模型:
public class clsMain
{
public string[] selectedAnswer { get; set; }
public List<ClsQuestions> lstQuestion { get; set; }
public List<ClsOptions> lstOptions { get; set; }
}
public class ClsQuestions
{
public string question { get; set; }
}
public class ClsOptions
{
public int optionid { get; set; }
public string optionvalue { get; set; }
public string optionlable { get; set; }
}
控制器
[HttpPost]
public ActionResult FromSelectedValues(clsMain model)
{
return View();
}
查看
@for (int i = 0; i < 2; i++){
Question @i
@Html.RadioButtonFor(m => m.SelectedAnswer[i], "Answer1"+i)
<label>Answer1 @i</label>
@Html.RadioButtonFor(m => m.SelectedAnswer[i], "Answer2"+i)
<label>Answer2 @i</label>
@Html.RadioButtonFor(m => m.SelectedAnswer[i], "Answer3"+i)
<label>Answer3 @i</label>
@Html.RadioButtonFor(m => m.SelectedAnswer[i], "Answer4"+i)
<label> Answer4 @i</label>
}
简而言之,我想在控制器中获得选定的选项。
【问题讨论】:
-
你想要Post方法中单选按钮的值吗?
-
是选择的值
-
你调试过
SelectedAnswer数组里面的内容了吗?发布后? -
并请在问题中提供代码而不是易于重现问题或查看实际工作的图像
-
你的模型错了——你需要一个(比如)
class QuestionVM属性为List<ClsOptions> PossibleAnswers和[Required]string Selected Answer,然后你将一个视图模型的列表传递给视图
标签: c# asp.net-mvc asp.net-mvc-4 razor radiobuttonlist