我httppost后出错 - 在点击按钮后知道下拉列表

时间:2015-12-02 21:35:59

标签: c# asp.net-mvc

当我点击我的身体并取一个值时,单击该按钮,所以当点击它时所以犯了这个错误。

我已尝试过很多东西,现在在我的代码后面下拉,但它仍然给我同样的错误。我试着看看它是如何发生的。

  

具有键' SelectedOpgaveValue'的ViewData项。属于' System.String'但必须属于' 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"

我的控制器:

        [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);
    }

型号:

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>
}

1 个答案:

答案 0 :(得分:0)

发生错误是因为当您在POST方法中返回视图时,OpgaveValueList的值为null(并且DropDownListFor()方法会回退到需要第一个参数的重载成为IEnumerable。您需要在返回视图之前重新分配OpgaveValueList的值,就像在GET方法中一样。

另请注意,从第一个IEnumerable<SelectListItem>(这是SelectList)中发出第二个List<SelectListItem> items = new List<SelectListItem>(); foreach (var item in QA.ToList()) { items.Add(new SelectListItem() { Text = item.text, Value = item.id.ToString() }); } model.OpgaveValueList = items; return View(model); 的额外开销是没有意义的。您的代码可以简单地

model.OpgaveValueList = QA.Select(x => new SelectListItem
{
  Text = x.text,
  Value = x.id.ToString()
};

甚至更简单

private void ConfigureViewmodel(OpgaverPage model)
{
  model.OpgaveValueList = db.Question_Answers.Where(i => i.fk_Question == A.id.Select(x => new SelectListItem
  {
    Text = x.text,
    Value = x.id.ToString()
  };
}

为了保持干燥,我建议你创建一个私人方法

ConfigureViewmodel(model);
return View(model);

然后在GET和POST方法中,在返回视图之前调用它

<script>