0

I am really struggling with Html.DropdownList. Identical code worked under VS2012 but fails with the subject error under VS2013. Any help would be deeply appreciated.

Controller (selectedCredit is int):

var creditQuery = from d in db.CreditStatus
                    orderby d.CreditId
                    select new { d.CreditId, d.Reason };
ViewBag.Credit = new SelectList(creditQuery.ToList(), "CreditId", "Reason", selectedCredit);

View:

@Html.DropDownList("Credit", null, new { @class = "form-control" })

If the second parm of Html.DropdownList is not null, it will ignore selectedCredit and no item will be selected.

List list shows correctly but fails on Post with the subject error. HELP!

4

2 回答 2

2

如果您的 POST 控制器不执行重定向 GET,则您必须再次重新填充 ViewBag,因为您正在显示相同的视图。

因此,在您的 Post 控制器中,将相同的值放入ViewBag.Credit

于 2013-10-27T11:52:34.133 回答
1

一旦你对服务器进行 POST,就需要重新填充 viewbag

将此插入您的帖子操作中

var creditQuery = from d in db.CreditStatus
                orderby d.CreditId
                select new { d.CreditId, d.Reason };
ViewBag.Credit = new SelectList(creditQuery.ToList(), "CreditId", "Reason", selectedCredit);
于 2013-10-27T11:59:25.867 回答