when I click on my body and take a value, click the button, so when clicked on it so make this one mistake.
I've tried many things now drop down and inside behind my code but it still gives me the same error. I try the bebug and see how it happens.
The ViewData item that has the key 'SelectedOpgaveValue' is of type 'System.String' but must be of type 'IEnumerable'.
Line 48:
Line 49: <div class="form-group">
Line 50: @Html.DropDownListFor(x => x.SelectedOpgaveValue, Model.OpgaveValueList, new
Line 51: {
Line 52: @class = "form-control"
My controller:
[MvcApplication.SessionExpire]
[HttpGet]
public ActionResult Index()
{
DataLinqDB db = new DataLinqDB();
OpgaverPage model = new OpgaverPage();
var random = new Random();
var antalopgaver = db.Questions.Count();
var number = random.Next(antalopgaver);
//henter en random spørgsmål.
var A = db.Questions.Where(i => i.fk_Categories == 1).Skip(number).Take(1).FirstOrDefault();
model.Overskift = A.title;
model.HiddenId = A.id;
//Henter alle de svar muligheder man har til den id som man få fra A
List<Question_Answer> QA = db.Question_Answers.Where(i => i.fk_Question == A.id).ToList();
//dropdown her
List<SelectListItem> items = new List<SelectListItem>();
foreach (var item in QA.ToList())
{
items.Add(new SelectListItem() { Text = item.text, Value = item.id.ToString() });
}
//seleced dropdown
model.OpgaveValueList = new SelectList(items, "Value", "Text");
return View(model);
}
[MvcApplication.SessionExpire]
[HttpPost]
public ActionResult index(OpgaverPage opgavervalue)
{
DataLinqDB db = new DataLinqDB();
if (ModelState.IsValid)
{
var opgaver = db.Question_Answers.FirstOrDefault(i => i.fk_Question == opgavervalue.HiddenId);
if (opgaver != null)
{
if (opgavervalue.SelectedOpgaveValue.ToString() == opgaver.id.ToString() && opgaver.er_svaret == true)
{
PointHelper.Point.PointInsert("opgaver");
}
else
{
//error
ModelState.AddModelError("", "Desværre dit svar forkert");
}
}
else
{
return RedirectToAction("index");
}
}
return View(opgavervalue);
}
Model:
public class OpgaverPage
{
public string Overskift { get; set; }
public int HiddenId { get; set; }
[Display(Name ="Spørgsmål")]
public string Svar { get; set; }
public List<Question_Answer> QuestionList { get; set; }
public IEnumerable<SelectListItem> OpgaveValueList { get; set; }
public string SelectedOpgaveValue { get; set; }
}
index.cshtml
<p>@Model.Overskift</p>
@using (Html.BeginForm("index", "Opgaver"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(i => i.HiddenId)
<div class="form-group">
// Error is here!!
@Html.DropDownListFor(x => x.SelectedOpgaveValue, Model.OpgaveValueList, new { @class = "form-control" })
</div>
<button type="submit" class="btn btn-effect-ripple btn-success"><i class="fa fa-check"></i> Tjek mit svar</button>
}
SelectList- i.e.model.OpgaveValueList = new SelectList(items, "Value", "Text");as you did in the GET method