如何在SelectList/@Html.DropDownList中设置SelectListItem?

时间:2011-10-02 21:31:37

标签: asp.net-mvc-3

如何根据以下代码设置SelectList/@Html.DropDownList的值?

- 控制器:

     public ActionResult Edit(int id)
    {
        Order o = db.Orders.Find(id);

        ViewBag.OrderStatusTId = new SelectList(db.OrderStatusTIds, "OrderStatusTId", "Name", o.OrderStatusTId);  // I thought the last item would send in the selected item to the view?!?!?
        return View(o);
     }          

- 景观:

@Html.DropDownList("OrderStatusTId", (IEnumerable<SelectListItem>)ViewBag.OrderStatusTId, String.Empty)

1 个答案:

答案 0 :(得分:1)

使用viewdata代替viewbag并将帮助器更改为:

@ Html.DropDownList(“OrderStatusTId”,null,String.Empty)