开发者

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

开发者 https://www.devze.com 2023-04-10 08:01 出处:网络
How do I set the value of a SelectList/@Html.DropDownList given the following code? --Controller: public ActionResult Edit(int id)

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

精彩评论

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