Asp.net mvc 如何在投票系统中投票?
以下是我的POCO课程:Asp.net mvc 如何在投票系统中投票?,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,以下是我的POCO课程: public class PollOptions { public int Id { get; set; } public virtual Polls Polls { get; set; } public string Answer { get; set; } public int Votes { get; set; } } public class Polls {
public class PollOptions
{
public int Id { get; set; }
public virtual Polls Polls { get; set; }
public string Answer { get; set; }
public int Votes { get; set; }
}
public class Polls
{
public int Id { get; set; }
public string Question { get; set; }
public bool Active { get; set; }
public virtual ICollection<PollOptions> PollOptions { get; set; }
}
在索引视图中,我希望用户在下面投票
我的投票区是:
@section Polling{
@foreach (var question in Model.Polls)
{
<h3>@question.Question</h3>
<ul class="pollUnordedList">
@foreach(var answer in question.PollOptions)
{
<li><input type="radio" name="opt" id="@answer.Id" value="1"/><span>@answer.Answer</span></li>
}
</ul>
}
@using (Html.BeginForm(actionName: "Radio", controllerName: "Home",routeValues: new { id = 2, value = 5 }))
{
<p><input type="submit" class="btn btn-primary" value="ارسال نظر" /></p>
}
}
我如何获取pass as路由值的radioButton的值和id,例如,在上面的路由值中,id和value为2,5,请对所有单选按钮使用相同的名称:
@using (Html.BeginForm(actionName: "Radio", controllerName: "Home")) {
foreach (var question in Model.Polls) {
<h3>@question.Question</h3>
foreach (var answer in question.PollOptions) {
<label>
<input type="radio" name="answers" value="@answer.Id" />
@answer.Answer
</label>
}
}
<p><input type="submit" class="btn btn-primary" value="ارسال نظر" /></p>
}
编辑
根据你关于值总是等于1的评论,那么传递它是没有意义的。您只需假设每个选定单选按钮的值为1。您的操作方法如下所示:
[HttpPost]
public ActionResult Radio(int[] answers)
{
foreach (var id in answers)
{
var option = db.PollOptions.Find(id);
if (option != null) option.Votes++;
db.SaveChanges();
}
}
我使用jQuery AJAX解决了我的问题:
@using (Html.BeginForm(actionName: "Radio", controllerName: "Home")) {
foreach (var question in Model.Polls)
{
<h3>@question.Question</h3>
<ul class="pollUnordedList">
@foreach (var answer in question.PollOptions)
{
<li>
@*@Html.RadioButton("options",false,new {id=@answer.Id})<span>@answer.Answer</span>*@
@Html.RadioButton("options",answer.Id)<span>@answer.Answer</span>
</li>
}
</ul>
}
<p>
<input type="submit" id="btnVote" class="btn btn-primary" value="ارسال نظر" />
</p>
}
谢谢Daniel Liuzzi,不需要foreach,因为如果选中单选按钮,则在发布表单时提交的单选按钮的值。当您发布多个同名单选按钮时,它们将被发布为,例如,
answers=1&answers=2&answers=9&answers=17&……
MVC模型绑定然后将其转换为一个数组,例如answers={1,2,9,17,…}
然后,您可以在此数组上执行foreach
循环,以将选定的答案保存到DB.tnx,但我不知道如何通过HTML.BeginForm传递它们,您能解释一下吗?我现在已将HTML.BeginForm
添加到示例中。请注意,您的单选按钮都需要位于表单中,否则它们的值将不会被发布。如我说过,我想在我的方法中发布两个参数:1)Id 2)投票,因为通过Id我可以在我的表格中更新投票(投票是1,事实上它就像一个计数器)。恐怕你当时的描述不清楚。查看您提供的代码示例,首先有一组问题,这些问题又有一组选项。我不知道选票从哪里来。在你的帖子中唯一提到的投票(标题除外)是opt.voces+=value
,我不确定它的作用。对不起,不清楚。如果您可以更新您的问题并用更一致的术语解释自己,我可能会更好地理解您并为您提供进一步的建议。关于value=“1”
,此值是否始终等于1?除非您的视图只有一个单选按钮,否则您肯定需要foreach
循环。当您的表单发布时,所有单选按钮都会发布,这将转换为操作方法上的数组,如我在回答的第二条注释中所解释的。
[HttpPost]
public ActionResult Radio(int[] answers)
{
// TODO: Save your answers
}
[HttpPost]
public ActionResult Radio(int[] answers)
{
foreach (var id in answers)
{
var option = db.PollOptions.Find(id);
if (option != null) option.Votes++;
db.SaveChanges();
}
}
@using (Html.BeginForm(actionName: "Radio", controllerName: "Home")) {
foreach (var question in Model.Polls)
{
<h3>@question.Question</h3>
<ul class="pollUnordedList">
@foreach (var answer in question.PollOptions)
{
<li>
@*@Html.RadioButton("options",false,new {id=@answer.Id})<span>@answer.Answer</span>*@
@Html.RadioButton("options",answer.Id)<span>@answer.Answer</span>
</li>
}
</ul>
}
<p>
<input type="submit" id="btnVote" class="btn btn-primary" value="ارسال نظر" />
</p>
}
[HttpPost]
public ActionResult Radio(int options)
{
var option = db.PollOptions.Find(options);
if (option != null) option.Votes++;
db.SaveChanges();
return RedirectToAction("Index");
}