开发者

How to set SelectListItem in SelectList/@Html.DropDownList?

How do I set the value of a SelectList/@Html.DropDownList given the following code?

--Controller:

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

--View:

@Html.DropDownList("OrderStatusTId", (IEnumerable<SelectListItem>)ViewBag.Or开发者_StackOverflowderStatusTId, String.Empty)


Use viewdata instead of viewbag and change your helper to:

@Html.DropDownList("OrderStatusTId",null, String.Empty)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜