我在这里有起作用的代码段,我正在编写一个ASP.NET MVC应用程序。下面是HTML和背后的代码。我需要有关如何一次只为每个问题选择一个单选按钮以及其次如何保存用户响应的帮助。
查看
@using (Html.BeginForm("Save", "Home", FormMethod.Post))
{
<div class="jumbotron">
@for (int i = 0; i < Model.Count(); i++)
{
var inputTypes = Model[i].InputTypes.Split(',').ToList();
<p>@Model[i].Details</p>
for (int j = 0; j < Model[i].Options.Count; j++)
{
for (int x = 0; x < inputTypes.Count; x++)
{
if (inputTypes[x] == "TextBox")
{
@Html.TextBoxFor(u => Model[i].Options[j].Details, new { @class = "form" })
}
else if (inputTypes[x] == "CheckBox")
{
@Html.CheckBoxFor(u => Model[i].Options[j].Details, new { @class = "form" })
}
else if (inputTypes[x] == "RadioButton")
{
@Html.RadioButtonFor(u => Model[i].Options[j].Details, new { @class = "form" })
}
}
}
}
</div>
}
控制器
public ActionResult Index()
{
var questions = _context.Questions.ToList();
var questionModel = new List<Question>();
questions.ForEach(q =>
{
var model = new Question
{
Id = q.Id,
Title = q.Title,
Details = q.Details,
HasMultiAnswers = q.HasMultiAnswers,
NumberOfAnswers = q.NumberOfAnswers,
InputTypes = q.InputTypes, //New Line
Options = _context.Options.Where(f => f.point == 10).ToList()
};
questionModel.Add(model);
});
return View(questionModel);
}
模型
public class Question
{
public long Id { get; set; }
public string Title { get; set; }
public string Details { get; set; }
public bool HasMultiAnswers { get; set; }
public int NumberOfAnswers { get; set; }
public double TotalPoint { get; set; }
public String InputTypes { get; set; }
public System.Collections.Generic.List<Option> Options { get; set; }
}
public class Option
{
public int Id { get; set; }
public int Order { get; set; }
public string Details { get; set; }
public double point { get; set; }
}
更新:模型已针对代码进行了更新。
---回复后,我已经更新了代码
@model List<Demo.Controllers.HomeController.MModel>
@using (Html.BeginForm("Save", "Home", FormMethod.Post))
{
<div class="jumbotron">
@for (int i = 0; i < Model.Count(); i++)
{
var inputTypes = Model[i].InputTypes.Split(',').ToList();
<p> @Model[i].Details</p>
for (int j = 0; j < Model[i].Options.Count; j++)
{
for (int x = 0; x < inputTypes.Count; x++)
{
if (inputTypes[x] == "TextBox")
{
@Html.TextBoxFor(u => Model[i].Options[j].Name, new { @class = "form-control" })
<br />
}
else if (inputTypes[x] == "CheckBox")
{
@Html.CheckBoxFor(u => Model[i].Options[j].IsBoolean.Value, new { @class = "form-control", @checked = "false" })
<br />
}
else if (inputTypes[x] == "RadioButton")
{
@Html.RadioButtonFor(u => Model[i].Options[j].IsBoolean, "false", new { @class = "form-control" })
<br />
}
else if (inputTypes[x] == "Multiple")
{
@Html.CheckBoxFor(u => Model[i].Options[j].IsBoolean.Value, new { @class = "form-control", @value = "false" })
<br />
@Html.TextBoxFor(u => Model[i].Options[j].Name, new { @class = "form-control" })
<br />
}
}
}
}
</div>
}
public ActionResult Index()
{
var questions = _context.aQuestions.ToList();
var questionModel = new List<MModel>();
questions.ForEach(q =>
{
var model = new MModel
{
QuestionID = q.QuestionID,
Title = q.Title,
Details = q.Details,
HasMultiAnswers = q.HasMultiAnswers,
NumberOfAnswers = q.NumberOfAnswers,
InputTypes = q.InputTypes, //New Line
Options = _context.aOptions.Where(f => f.QuestionID == q.QuestionID).ToList()
};
questionModel.Add(model);
});
return View(questionModel);
}
public class MModel
{
public short QuestionID { get; set; }
public string Title { get; set; }
public string Details { get; set; }
public Nullable<bool> HasMultiAnswers { get; set; }
public Nullable<byte> NumberOfAnswers { get; set; }
public string InputTypes { get; set; }
public System.Collections.Generic.List<aOption> Options { get; set; }
}
答案 0 :(得分:0)
单选按钮通常用于返回多值列表中的选择。要仅选择一个单选按钮,必须将“单选按钮”组绑定到单个模型属性。 示例:
count = 0
while(y<=nrow(matrixold)){
if(matrixold[y,1]==1){
count = count + 1
matrixnew[count,] <<- rbind(matrixold[y,])
}
y <- y+1
}
其中x表示在此示例中具有@Html.RadioButtonFor(x => x.Answer, new { value = "a" })
@Html.RadioButtonFor(x => x.Answer, new { value = "b" })
@Html.RadioButtonFor(x => x.Answer, new { value = "c" })
属性的模型。现在,根据用户选择,Answer
值将设置为a,b或c