大佬教程收集整理的这篇文章主要介绍了ASP.NET MVC 5组单选按钮,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
您的代码存在许多问题,包括生成重复ID
的(无效的HTML),生成重复的name
属性(这就是为什么您仅创建一组的原因,但更重要的是,这将阻止您在回发时绑定到模型),那么您实际上并没有绑定到有效的属性。
您将需要创建视图模型来表示要显示和编辑的视图,并在for
循环中(或使用EditorTemplate
)生成单选按钮,以便使用索引器正确命名它们。
查看模型
public class QuestionVM { public int id { get; set; } // for binding public String Text { get; set; } [required] public int? SELEctedAnswer { get; set; } // for binding public IEnumerable<AnswerVM> PossibleAnswers { get; set; } } public class SubjectVM { public int? ID { get; set; } [displayFormat(NulldisplayText = "General")] public String name { get; set; } public List<QuestionVM> Questions { get; set; } } public class AnswerVM { public int id { get; set; } public String Text { get; set; } } public class studentVM { public int id { get; set; } public String name { get; set; } // plus any other propertIEs of student that you want to display in the vIEw public List<SubjectVM> Subjects { get; set; } }
@H_772_19@视图
@model YourAssembly.studentVM @using(HTML.beginForm()) { @HTMl.HIDdenFor(m => m.ID) @HTMl.displayFor(m => m.Name) for(int i = 0; i < Model.Subjects.Count; i++) { @HTMl.HIDdenFor(m => m.Subjects[i].ID) @HTMl.displayFor(m => m.Subjects[i].Name) // will display "General" if no name for (int j = 0; j < Model.Subjects[i].Questions.Count; j++) { @HTMl.HIDdenFor(m => m.Subjects[i].Questions[j].ID) @HTMl.displayFor(m => m.Subjects[i].Questions[j].Text) foreach(var answer in Model.Subjects[i].Questions[j].PossibleAnswers ) { <div> @HTMl.RadiobuttonFor(m => m.Subjects[i].Questions[j].SELEctedAnswer, answer.ID, new { ID = answer.ID}) <label for="@answer.ID">@answer.Text</label> </div> } @HTMl.ValIDationmessageFor(m => m.Subjects[i].Questions[j].SELEctedAnswer) } } <input type="submit" value="save" /> }
@H_772_19@控制者
public ActionResult Edit(int id) { studentVM model = new studentVM(); // populate your vIEw model with values from the database return VIEw(model); } [httpPost] public ActionResult Edit(studentVM model) { // save and redirect }
@H_772_19@请注意,我通过你的模型所隐含的数据库结构混淆了一点(比如你为什么需要有单独的模型
Question
,并SubjectQuestion
当null
了数值SubjectID
将其标识为“一般”的问题)。我建议您首先在GET方法中对一些值进行硬编码,以了解其工作原理并回发。studentVM model = new studentVM(); model.ID = 1; model.name = "bambiinela"; model.Subjects = new List<SubjectVM>() { new SubjectVM() { Questions = new List<QuestionVM>() { new QuestionVM() { ID = 1, Text = "Question 1", SELEctedAnswer = ?, // set this if you want to preSELEct an option PossibleAnswers = new List<AnswerVM>() { new AnswerVM() { ID = 1, Text = "Answer A" }, new AnswerVM() { ID = 1, Text = "Answer B" } } }, new QuestionVM() { ID = 2, Text = "Question 2", PossibleAnswers = new List<AnswerVM>() { // SIMILAR TO above } } } }, new SubjectVM() { ID = 1, name = "Math", Questions = new List<QuestionVM>() { // SIMILAR TO above } } };
@H_772_19@发布时,将为模型填充每个主题中每个问题的所选答案的ID。注意
displayFor()
某些属性的使用。这些不会回发,因此如果您返回视图(例如@H_808_3@modelState,无效),则需要重新填充这些属性。或者,您可以生成一个只读文本框或为这些属性添加隐藏的输入。我还建议您检查生成的HTML,尤其是看起来像这样的name属性<input type="radio" name="Subjects[0].Questions[0].SELEctedAnswer" ...
@H_772_19@让您了解回发后集合如何绑定到模型
解决方法
我正在开始我的第一个ASP.NET MVC项目,所以我有一个简单的问题。我有以下代码:
foreach(var question in Model.GeneralQuestions) { <div class = "well"> <h3> <strong>@question.QuestionString</strong> </h3> @foreach (var answer in question.PossibleAnswers) { @Html.RadioButtonFor(model => question.QuestionString,answer.Answer) @Html.Label(answer.Answer) <br /> } </div> }
@H_772_19@ @H_459_2@model.GeneralQuestions中的所有问题都是唯一的,因此单选按钮应按名称属性分为几组(对于每个问题,一组单选按钮)。但是此代码仅产生一组,因此当我回答第二个问题时,第一个被取消选择。我需要更改什么?编辑
我的模型看起来像:public class studentViewModel { public student student { get; set; } public List<Question> GeneralQuestions { get; set; } public List<SubjectQuestions> SubjectQuestions { get; set; } } public class student { public int studentID { get; set; } public String Index { get; set; } public String Name { get; set; } public String Surname { get; set; } public virtual ICollection<Subject> Subjects { get; set; } } public class Question { public int QuestionID { get; set; } public String QuestionString { get; set; } public bool IsAssociatedWithSubject { get; set; } public virtual ICollection<PossibleAnswer> PossibleAnswers { get; set; } public virtual ICollection<Results> Results { get; set; } } public class SubjectQuestions { public Subject Subject { get; set; } public List<Question> Questions { get; set; } } public class Results { public int ResultsID { get; set; } public int QuestionID { get; set; } public int? SubjectID { get; set; } public int PossibleAnswerID { get; set; } public virtual Question Question { get; set; } public virtual PossibleAnswer PossibleAnswer { get; set; } public virtual Subject Subject { get; set; } }
@H_772_19@在一个studentViewModel实例中,我保存了一个学生和他应该回答的所有问题(包括与他正在研究的学科有关的一般问题),并将其传递给视图。我认为所有问题都以单一形式提出,它们都是无线电的类型。因此,有人可以帮助我将单选按钮分组并正确发回此表单吗?
大佬总结
以上是大佬教程为你收集整理的ASP.NET MVC 5组单选按钮全部内容,希望文章能够帮你解决ASP.NET MVC 5组单选按钮所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。