I want to show a list of radiobuttons from a model using Entity Framework and MVC 5. The list is displayed with the radiobuttons, but the value of the radiobuttons is always te same for al rows and I can't find a fault in the code. Perhaps someone here have an idea?
Code:
Model:
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 });
}
View:
@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")
Output 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>
As you can see in the comment in the Controller, isSufficient have always the value 'Beoordeling.Voldoende'. Can you help me?
I have found a solution. Instead of RadionbuttonFor(isSufficient) I now use Result. I don't now if it is a good solution, but it works!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.