2015-04-01 44 views
0

我想使用實體框架和MVC 5來顯示模型中的單選按鈕列表。 該列表以單選按鈕顯示,但單選按鈕的值對於al行總是相同我在代碼中找不到錯誤。 也許有人在這裏有一個想法?來自列表中的模型的ASP MVC Radiobutton

代碼:

型號:

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> 

正如您在控制器的註釋中看到的,isSufficient始終具有值'Beoordeling.Voldoende'。你可以幫我嗎?

+0

渲染源中單選按鈕的值是什麼? – 2015-04-01 14:40:24

+0

藉口我,我已經在原始文章中添加了生成的代碼。 – NVO 2015-04-01 14:57:53

+0

從方法中刪除'[Bind(Include =「Id,Result」)]''。您的排除屬性'isSufficient'從回發,所以它的默認值(這是'Voldoende')。同時刪除'new {@checked =「checked」,' - 使用強類型的幫助器,所選的按鈕將成爲'Beoordeling'的值 – 2015-04-01 21:18:09

回答

0

我找到了解決方案。而不是RadionbuttonFor(isSufficient)我現在使用Result。如果這是一個很好的解決方案,我現在不會這樣做,但它起作用!