mvc中 html是否单选按钮,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 PossibleAnswers { get; set; }}public class SubjectVM{

public int? ID { get; set; }

[DisplayFormat(NullDisplayText = "General")]

public string Name { get; set; }

public List 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 Subjects { get; set; }}

视图@model YourAssembly.StudentVM@using(Html.BeginForm()){

@Html.HiddenFor(m => m.ID)

@Html.DisplayFor(m => m.Name)

for(int i = 0; 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 

{

@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 )

{

@Html.RadioButtonFor(m => m.Subjects[i].Questions[j].SelectedAnswer, answer.ID, new { id = answer.ID})

@answer.Text

}

@Html.ValidationMessageFor(m => m.Subjects[i].Questions[j].SelectedAnswer)

}

}

}

调节器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}

注意我对模型隐含的数据库结构感到有些困惑(例如,为什么需要单独的模型Question以及SubjectQuestion何时将null值SubjectID标识为“常规”问题)。我建议你首先在GET方法中对一些值进行硬编码,看看它是如何工作的,然后回发。StudentVM model = new StudentVM();model.ID = 1;model.Name = "bambiinela";model.Subjects = new List(){

new SubjectVM()

{

Questions = new List()

{

new QuestionVM()

{

ID = 1,

Text = "Question 1",

SelectedAnswer = ?, // set this if you want to preselect an option

PossibleAnswers = new List()

{

new AnswerVM()

{

ID = 1,

Text = "Answer A"

},

new AnswerVM()

{

ID = 1,

Text = "Answer B"

}

}

},

new QuestionVM()

{

ID = 2,

Text = "Question 2",

PossibleAnswers = new List()

{

// similar to above

}

}

}

},

new SubjectVM()

{

ID = 1,

Name = "Math",

Questions = new List()

{

// similar to above

}

}};

发布时,模型将填充每个主题中每个问题的所选答案的ID。请注意DisplayFor()某些属性的用法。这些不会回发,因此如果您返回视图(例如ModelState无效),则需要重新填充这些属性。或者,您可以生成只读文本框或为这些属性添加隐藏输入。我还建议你检查生成的HTML,特别是看起来像的名称属性

让您了解集合如何在回发后绑定到您的模型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值