我如何根据项目外键值对我的@Html.RadioButtonFor 进行分组
How i can group my @Html.RadioButtonFor according to the Item foreign key value
我的 asp.net mvc-5 网络应用程序中有 2 个模型:-
public partial class Anwer
{
public Anwer()
{
this.UserFormsAnswers = new HashSet<UserFormsAnswer>();
}
public int Id { get; set; }
public string AnwerDesc { get; set; }
public int QuestionID { get; set; }
public bool Correct { get; set; }
public virtual Question Question { get; set; }
public virtual ICollection<UserFormsAnswer> UserFormsAnswers { get; set; }
}
public partial class Question
{
public Question()
{
this.Anwers = new HashSet<Anwer>();
}
public int Id { get; set; }
public string QuestionDesc { get; set; }
public int Order { get; set; }
public bool Active { get; set; }
public virtual ICollection<Anwer> Anwers { get; set; }
}
现在我想在视图中显示所有问题,并为每个问题在单选按钮中显示其答案,所以我尝试了以下操作:-
@model IEnumerable<QuestionsDemo.Models.Question>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@{int i = 0;
foreach (var item in Model)
{
<div class="form-group">
@(i+1) <spn>-</spn> @Html.DisplayFor(modelItem => item.QuestionDesc) <br />
@foreach (var answer in item.Anwers)
{
<div class="col-md-10">
@Html.RadioButtonFor(modelitem2 => answer.QuestionID, answer.Id) @answer.AnwerDesc <br />
</div>
}
</div><hr/>
}
}
</div>
}
我认为一个问题的所有答案的单选按钮都具有相同的名称。所以用户每个问题只能select一个选项。但似乎所有答案单选按钮的名称如下:-
<input id="answer_QuestionID" name="answer.QuestionID" type="radio" value="1" /> 10 <br />
所以有人可以就此提出建议吗?我如何对每个问题的单选按钮进行分组?
第二个问题。我如何 post 将所有答案 ID 返回到我的 post 操作方法?我应该使用索引器吗??
您不能使用 foreach
循环为 Question
生成表单控件。您需要使用 for
循环,或者更好地为 typeof Question
使用自定义 EditorTemplate
。详细解释请参考。
您的 Question
模型还需要一个 属性 来将选定的 Answer
绑定到,例如
public class Question
{
public int Id { get; set; }
....
public int SelectedAnswer { get; set; }
}
请注意,您正在编辑数据,因此您应该为 Question
和 Answer
创建视图模型,而不是在您的视图中使用数据模型。
使用以下代码创建局部视图Views/Shared/EditorTemplates/QuestionVM.cshtml
@model QuestionsDemo.Models.Question
@Html.HiddenFor(m => m.Id)
// Add additional hidden inputs for any properties of Question that you want to be posted
@Html.DisplayFor(m => m.QuestionDesc)
@foreach(var answer in Model.Anwers)
{
<label>
@Html.RadioButtonFor(m => m.SelectedAnswer, answer.Id, new { id = "" })
<span>@answer.AnwerDesc</span>
</label>
}
那么主视图将是
@model IEnumerable<QuestionsDemo.Models.Question>
....
@using (Html.BeginForm())
{
....
@Html.EditorFor(m => m)
<input type="submit" value="Save" />
}
请注意 EditorFor()
方法将为集合中的每个项目正确生成 html。
我的 asp.net mvc-5 网络应用程序中有 2 个模型:-
public partial class Anwer
{
public Anwer()
{
this.UserFormsAnswers = new HashSet<UserFormsAnswer>();
}
public int Id { get; set; }
public string AnwerDesc { get; set; }
public int QuestionID { get; set; }
public bool Correct { get; set; }
public virtual Question Question { get; set; }
public virtual ICollection<UserFormsAnswer> UserFormsAnswers { get; set; }
}
public partial class Question
{
public Question()
{
this.Anwers = new HashSet<Anwer>();
}
public int Id { get; set; }
public string QuestionDesc { get; set; }
public int Order { get; set; }
public bool Active { get; set; }
public virtual ICollection<Anwer> Anwers { get; set; }
}
现在我想在视图中显示所有问题,并为每个问题在单选按钮中显示其答案,所以我尝试了以下操作:-
@model IEnumerable<QuestionsDemo.Models.Question>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@{int i = 0;
foreach (var item in Model)
{
<div class="form-group">
@(i+1) <spn>-</spn> @Html.DisplayFor(modelItem => item.QuestionDesc) <br />
@foreach (var answer in item.Anwers)
{
<div class="col-md-10">
@Html.RadioButtonFor(modelitem2 => answer.QuestionID, answer.Id) @answer.AnwerDesc <br />
</div>
}
</div><hr/>
}
}
</div>
}
我认为一个问题的所有答案的单选按钮都具有相同的名称。所以用户每个问题只能select一个选项。但似乎所有答案单选按钮的名称如下:-
<input id="answer_QuestionID" name="answer.QuestionID" type="radio" value="1" /> 10 <br />
所以有人可以就此提出建议吗?我如何对每个问题的单选按钮进行分组?
第二个问题。我如何 post 将所有答案 ID 返回到我的 post 操作方法?我应该使用索引器吗??
您不能使用 foreach
循环为 Question
生成表单控件。您需要使用 for
循环,或者更好地为 typeof Question
使用自定义 EditorTemplate
。详细解释请参考
您的 Question
模型还需要一个 属性 来将选定的 Answer
绑定到,例如
public class Question
{
public int Id { get; set; }
....
public int SelectedAnswer { get; set; }
}
请注意,您正在编辑数据,因此您应该为 Question
和 Answer
创建视图模型,而不是在您的视图中使用数据模型。
使用以下代码创建局部视图Views/Shared/EditorTemplates/QuestionVM.cshtml
@model QuestionsDemo.Models.Question
@Html.HiddenFor(m => m.Id)
// Add additional hidden inputs for any properties of Question that you want to be posted
@Html.DisplayFor(m => m.QuestionDesc)
@foreach(var answer in Model.Anwers)
{
<label>
@Html.RadioButtonFor(m => m.SelectedAnswer, answer.Id, new { id = "" })
<span>@answer.AnwerDesc</span>
</label>
}
那么主视图将是
@model IEnumerable<QuestionsDemo.Models.Question>
....
@using (Html.BeginForm())
{
....
@Html.EditorFor(m => m)
<input type="submit" value="Save" />
}
请注意 EditorFor()
方法将为集合中的每个项目正确生成 html。