0
我有这个观点。除此之外,我想补充:onclick="javascript:SubmitClick(@Model.id, answer.Id);"
来定义RadioButtons。如何添加onclick事件到剃刀视图中的单选按钮
@model WebApplication2.Models.Question
<div>
@Html.HiddenFor(x => x.Id)
<h3> @Model.QuestionText </h3>
@foreach (var answer in Model.Answers) {
<p>
@Html.RadioButtonFor(b => b.SelectedAnswer, answer.Id) @answer.AnswerText
</p>
}
</div>
问题与答案是这样的模型。
public class Question {
public int Id { set; get; }
public string QuestionText { set; get; }
public virtual ICollection<Answer> Answers { set; get; }
public string SelectedAnswer { set; get; } //this field is SET after clicking the radio button, I don't need this field ant this behaviour
}
public class Answer {
public int Id { set; get; }
public string AnswerText { set; get; }
}
据我所知b => b.SelectedAnswer
内@Html.RadioButtonFor
这种表达使得该点击后场SelectedAnswer
在Question
类将被设置。我不需要,我只需要在foreach循环中只有一个单选按钮可以同时选中,点击它们就会调用onclick="javascript:SubmitClick(@Model.id, answer.Id);"
。也可以以过时的方式完成,只是为了使其工作。
屏幕,这将有助于已了解视图的外观:
为什么不把它放在表单元素中并回传? 'SubmitClick'函数有什么作用? – 2014-09-01 23:15:00
@StephenMuecke回发重定向我做不同的行动?我发布了一个适合我的答案。如果你知道更简单的方法,请显示。谢谢。 – Yoda 2014-09-01 23:21:59
当然,如果你想。在你的post方法中,保存你的数据,然后'返回RedirectToAction(“SomeAction”);' – 2014-09-01 23:24:13