这些是我的 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 int Id { get; set; }
public string Question { get; set; }
public bool Active { get; set; }
public virtual ICollection<PollOptions> PollOptions { get; set; }
}
我已经通过这种方式将我的模型传递给索引视图:
public ActionResult Index()
{
var poll = db.Polls.Include("PollOptions").Where(x => x.Active).ToList();
return View(model);
}
在索引视图中,我希望用户投票,下面是我的投票部分:
@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>
}
}
我的行动方法:
public void Radio(int Id=0,int value=0)
{
PollOptions opt = db.PollOptions.Find(Id);
db.Entry(opt).State = System.Data.EntityState.Modified;
opt.Votes += value;
db.SaveChanges();
}
如何获取作为路由值传递的单选按钮的值和 id,例如在上面的路由值中,Id 和值的值为 2,5