获取多组单选按钮的选定值asp.net mvc 5
get selected value of multiple group of radio button asp.net mvc 5
我有一个页面有几个问题,每个问题都应该由几个用户回答,答案存储在 table 称为 QuestionReviews
我为每个用户使用以下代码 select 单选按钮
中的一个选项
foreach (var question in Model.Questions)
{
@Html.Hidden("QuestionId", question.QuestionId.ToString())
<div class="row m-0 bg-silver-light border">
<div class="col-12 bg-light pt-2 pb-2">
<h6>
@question.QuestionTitle
</h6>
<p class="text-secondary small m-0">
@question.QuestionBody
</p>
</div>
<div class="col-12 pt-2 pb-2">
<div class="form-check-inline">
<label class="form-check-label" for="radio2">
<input name="@("q"+question.SeminarQuestionId)" type="radio" class="form-check-input" id="radio2" value="20">20
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio3">
<input name="@("q"+question.SeminarQuestionId)" type="radio" class="form-check-input" id="radio3" value="40">40
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio4">
<input name="@("q"+question.SeminarQuestionId)" type="radio" class="form-check-input" id="radio4" value="60">60
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio5">
<input name="@("q"+question.SeminarQuestionId)" type="radio" class="form-check-input" id="radio5" value="80">80
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio6">
<input name="@("q"+question.SeminarQuestionId)" type="radio" class="form-check-input" id="radio6" value="100">100
</label>
</div>
</div>
</div>
}
这是我的操作方法:
foreach (string key in Request.Form.Keys)
{
if (key == "QuestionId")
{
int qId = Convert.ToInt32(Request.Form[key]);
var q = _dbContext.Questions.FirstOrDefault(x => x.QuestionId == qId);
var reviews = _dbContext.QuestionReviews.Where(x => x.QuestionId == qId).ToList();
foreach (var review in reviews)
{
var k = "q" + review.QuestionId; /// get q1,q2 and ...
review.ReviewPoint =Convert.ToInt32(Request.Form[k]);
}
}
}
用户点击提交按钮后,我应该在控制器中获得问题 ID 和答案值的列表
我的问题是用户应该同时回答几个问题,我不知道如何在 controller
中为每个问题获取 questionId 和 selected 无线电值
如果您将视图中的 foreach
循环转换为 for
循环,您可以通过对输入名称使用相同的索引 i
来关联您的值:
@for (int i = 0; i < Model.Questions.Count(); i++)
{
@Html.HiddenFor(model => model.Questions[i].QuestionId)
<div class="row m-0 bg-silver-light border">
<div class="col-12 bg-light pt-2 pb-2">
<h6>
@Model.Questions[i].QuestionTitle
</h6>
<p class="text-secondary small m-0">
@Model.Questions[i].QuestionBody
</p>
</div>
<div class="col-12 pt-2 pb-2">
<div class="form-check-inline">
<label class="form-check-label" for="radio2">
@Html.RadioButtonFor(model => model.Questions[i].Answer, 20, htmlAttributes: new { @class = "form-check-input", @id = "radio2"})
20
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio3">
@Html.RadioButtonFor(model => model.Questions[i].Answer, 40, htmlAttributes: new { @class = "form-check-input", @id = "radio3" })
40
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio4">
@Html.RadioButtonFor(model => model.Questions[i].Answer, 60, htmlAttributes: new { @class = "form-check-input", @id = "radio4" })
60
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio5">
@Html.RadioButtonFor(model => model.Questions[i].Answer, 80, htmlAttributes: new { @class = "form-check-input", @id = "radio5" })
80
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio6">
@Html.RadioButtonFor(model => model.Questions[i].Answer, 100, htmlAttributes: new { @class = "form-check-input", @id = "radio6" })
100
</label>
</div>
</div>
</div>
}
并假设您的 ViewModel 有一个 属性 Questions
,像这样:
public class QuestionReviewViewModel
{
public List<QuestionModel> Questions { get; set; } = new List<QuestionModel>();
}
您可以将您的 QuestionId 和 Answer 值绑定到您的 QuestionModel:
public class QuestionModel
{
public int QuestionId { get; set; }
public int Answer { get; set; }
}
只需在 HttpPost
操作中绑定到 ViewModel:
[HttpPost]
public ActionResult PostAction(QuestionReviewViewModel vm)
{
if (ModelState.IsValid)
{
for (int i = 0; i < vm.Questions.Count; i++)
{
int id = vm.Questions[i].QuestionId;
int answer = vm.Questions[i].Answer;
}
return Content("Something good");
}
else
{
return Content("Something rotten");
}
}
Post back example
编辑
在这种情况下我会使用 SelectList -> https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.selectlist?view=aspnet-mvc-5.2
您可以将 SelectList 属性 添加到您的问题模型(请参阅上面的文档了解参数),它允许您设置选定值(在本例中为 Answer
):
// List of possible answers to this question
// populated from data source
public List<int> AnswerList = new List<int> { 20, 40, 60, 80, 100 };
// An MVC SelectList Class
public SelectList PossibleAnswers => new SelectList(AnswerList, Answer);
然后,在您看来,您可以循环遍历可能的答案,如果项目被选中,则有条件地应用选中的 属性:
<div class="col-12 pt-2 pb-2">
@foreach (var item in Model.Questions[i].PossibleAnswers)
{
var htmlAttr = new Dictionary<string, object>{{ "@class", "form-check-input" }};
if (item.Selected)
{
htmlAttr.Add("@checked", true);
}
<div class="form-check-inline">
<label class="form-check-label" for="radio2">
@Html.RadioButtonFor(model => model.Questions[i].Answer, item.Text, htmlAttributes: htmlAttr)
@item.Text
</label>
</div>
}
</div>
我有一个页面有几个问题,每个问题都应该由几个用户回答,答案存储在 table 称为 QuestionReviews
我为每个用户使用以下代码 select 单选按钮
foreach (var question in Model.Questions)
{
@Html.Hidden("QuestionId", question.QuestionId.ToString())
<div class="row m-0 bg-silver-light border">
<div class="col-12 bg-light pt-2 pb-2">
<h6>
@question.QuestionTitle
</h6>
<p class="text-secondary small m-0">
@question.QuestionBody
</p>
</div>
<div class="col-12 pt-2 pb-2">
<div class="form-check-inline">
<label class="form-check-label" for="radio2">
<input name="@("q"+question.SeminarQuestionId)" type="radio" class="form-check-input" id="radio2" value="20">20
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio3">
<input name="@("q"+question.SeminarQuestionId)" type="radio" class="form-check-input" id="radio3" value="40">40
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio4">
<input name="@("q"+question.SeminarQuestionId)" type="radio" class="form-check-input" id="radio4" value="60">60
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio5">
<input name="@("q"+question.SeminarQuestionId)" type="radio" class="form-check-input" id="radio5" value="80">80
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio6">
<input name="@("q"+question.SeminarQuestionId)" type="radio" class="form-check-input" id="radio6" value="100">100
</label>
</div>
</div>
</div>
}
这是我的操作方法:
foreach (string key in Request.Form.Keys)
{
if (key == "QuestionId")
{
int qId = Convert.ToInt32(Request.Form[key]);
var q = _dbContext.Questions.FirstOrDefault(x => x.QuestionId == qId);
var reviews = _dbContext.QuestionReviews.Where(x => x.QuestionId == qId).ToList();
foreach (var review in reviews)
{
var k = "q" + review.QuestionId; /// get q1,q2 and ...
review.ReviewPoint =Convert.ToInt32(Request.Form[k]);
}
}
}
用户点击提交按钮后,我应该在控制器中获得问题 ID 和答案值的列表 我的问题是用户应该同时回答几个问题,我不知道如何在 controller
中为每个问题获取 questionId 和 selected 无线电值如果您将视图中的 foreach
循环转换为 for
循环,您可以通过对输入名称使用相同的索引 i
来关联您的值:
@for (int i = 0; i < Model.Questions.Count(); i++)
{
@Html.HiddenFor(model => model.Questions[i].QuestionId)
<div class="row m-0 bg-silver-light border">
<div class="col-12 bg-light pt-2 pb-2">
<h6>
@Model.Questions[i].QuestionTitle
</h6>
<p class="text-secondary small m-0">
@Model.Questions[i].QuestionBody
</p>
</div>
<div class="col-12 pt-2 pb-2">
<div class="form-check-inline">
<label class="form-check-label" for="radio2">
@Html.RadioButtonFor(model => model.Questions[i].Answer, 20, htmlAttributes: new { @class = "form-check-input", @id = "radio2"})
20
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio3">
@Html.RadioButtonFor(model => model.Questions[i].Answer, 40, htmlAttributes: new { @class = "form-check-input", @id = "radio3" })
40
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio4">
@Html.RadioButtonFor(model => model.Questions[i].Answer, 60, htmlAttributes: new { @class = "form-check-input", @id = "radio4" })
60
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio5">
@Html.RadioButtonFor(model => model.Questions[i].Answer, 80, htmlAttributes: new { @class = "form-check-input", @id = "radio5" })
80
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio6">
@Html.RadioButtonFor(model => model.Questions[i].Answer, 100, htmlAttributes: new { @class = "form-check-input", @id = "radio6" })
100
</label>
</div>
</div>
</div>
}
并假设您的 ViewModel 有一个 属性 Questions
,像这样:
public class QuestionReviewViewModel
{
public List<QuestionModel> Questions { get; set; } = new List<QuestionModel>();
}
您可以将您的 QuestionId 和 Answer 值绑定到您的 QuestionModel:
public class QuestionModel
{
public int QuestionId { get; set; }
public int Answer { get; set; }
}
只需在 HttpPost
操作中绑定到 ViewModel:
[HttpPost]
public ActionResult PostAction(QuestionReviewViewModel vm)
{
if (ModelState.IsValid)
{
for (int i = 0; i < vm.Questions.Count; i++)
{
int id = vm.Questions[i].QuestionId;
int answer = vm.Questions[i].Answer;
}
return Content("Something good");
}
else
{
return Content("Something rotten");
}
}
Post back example
编辑
在这种情况下我会使用 SelectList -> https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.selectlist?view=aspnet-mvc-5.2
您可以将 SelectList 属性 添加到您的问题模型(请参阅上面的文档了解参数),它允许您设置选定值(在本例中为 Answer
):
// List of possible answers to this question
// populated from data source
public List<int> AnswerList = new List<int> { 20, 40, 60, 80, 100 };
// An MVC SelectList Class
public SelectList PossibleAnswers => new SelectList(AnswerList, Answer);
然后,在您看来,您可以循环遍历可能的答案,如果项目被选中,则有条件地应用选中的 属性:
<div class="col-12 pt-2 pb-2">
@foreach (var item in Model.Questions[i].PossibleAnswers)
{
var htmlAttr = new Dictionary<string, object>{{ "@class", "form-check-input" }};
if (item.Selected)
{
htmlAttr.Add("@checked", true);
}
<div class="form-check-inline">
<label class="form-check-label" for="radio2">
@Html.RadioButtonFor(model => model.Questions[i].Answer, item.Text, htmlAttributes: htmlAttr)
@item.Text
</label>
</div>
}
</div>