ASP MVC单选按钮来自列表中的模型

时间:2015-04-01 14:31:54

标签: c# asp.net asp.net-mvc asp.net-mvc-5 radio-button

我想使用Entity Framework和MVC 5显示模型中的radiobutton列表。 该列表与radiobuttons一起显示,但是对于al行,radiobuttons的值总是相同,并且我在代码中找不到故障。 也许这里有人有想法?

代码:

型号:

public class ResultIndexModel
{
    public IList<ResultInsertModel> resultList { get; set; }
}

public enum Beoordeling
{
    Voldoende, 
    Onvoldoende
}
public class ResultInsertModel
{
    public int UserId { get; set; }
    public int ExamId { get; set; }
    public int Id { get; set; }
    public Beoordeling isSufficient { get; set; }
    public Nullable<decimal> Result { get; set; }

    public Exam Exam { get; set; }
    public User User { get; set; }

}

Examscontroller:

[Authorize]
public ActionResult AddResults(int? id)
{
  if (id == null)
  {
     return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  }

  ViewBag.ExamName = ExamCatalog.GetName(id);

  return View(ExamCatalog.GetExamStudents(id));
}

[Authorize]
   [HttpPost]
   [ValidateAntiForgeryToken]
        public ActionResult AddResults([Bind(Include = "Id,Result")] List<ResultInsertModel> model)
        {
            int examId = 0;
            if (ModelState.IsValid)
            {
                foreach (var i in model)
                {
                    var c = db.ExamSubscription.Where(a => a.Id == i.Id).FirstOrDefault();
                    var e = db.Exam.Where(a => a.Id == c.ExamId).FirstOrDefault();

                    if (c != null)
                    {
                        if (!e.ResultIsDecimal)
                        {
                            if (i.isSufficient == Beoordeling.Voldoende)
                            {
//isSufficient have always the same value Beoordelig.Voldoende ?)
                                //Voldoende
                                c.Result = 10;
                            }
                            else
                            {
                                //onvoldoende
                                c.Result = 1;
                            }
                        }
                        else
                        {
                            c.Result = i.Result;
                        }
                    }

                    examId = c.ExamId;
                }

                //db.Entry(sub).State = EntityState.Modified;
                db.SaveChanges();

                //ResultsEntered vullen op examen
                var exam = db.Exam.Where(a => a.Id == examId).FirstOrDefault();

                if (exam.ResultsEntered == null)
                {
                    exam.ResultsEntered = System.DateTime.Now;

                    db.Entry(exam).State = EntityState.Modified;
                    db.SaveChanges();
                }


            }
            return RedirectToAction("Results", new { id = examId });
        }

查看:

@Html.RadioButtonFor(model => model[i].isSufficient, Beoordeling.Voldoende, new { @checked = "checked", id = "voldoende" + i })
                        @Html.Label("voldoende" + i, "Voldoende")
                        @Html.RadioButtonFor(model => model[i].isSufficient, Beoordeling.Onvoldoende, new { id = "onvoldoende" + i})
                        @Html.Label("onvoldoende" + i, "Onvoldoende")

输出HTML:

<input checked="checked" data-val="true" data-val-required="Het veld isSufficient is vereist." id="voldoende0" name="[0].isSufficient" type="radio" value="Voldoende" />
<label for="voldoende0">Voldoende</label>
<input id="onvoldoende0" name="[0].isSufficient" type="radio" value="Onvoldoende" />
<label for="onvoldoende0">Onvoldoende</label>

正如您在Controller中的注释中所看到的那样,isSufficient总是具有价值&#39; Beoordeling.Voldoende&#39;。你能救我吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我现在使用Result而不是RadionbuttonFor(isSufficient)。如果这是一个很好的解决方案,我现在不会,但它的确有效!