简体   繁体   中英

Dropdownlist Selected value does not work

I want to edit a record in my application, I go to edit page but I don't know why my dropdownlists don't show selected values that when I selected in create page. this is my codes :

My controller :

public ActionResult Edit(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    book book = db.books.Find(id);
    if (book == null)
    {
        return HttpNotFound();
    }

    ViewBag.books = new SelectList(db.books, "id", "name", book.value);
    return View(book);
}

and my action :

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.DropDownList("value", (IEnumerable<SelectListItem>)ViewBag.books , "Choose.")
}

What is problem? In trance I can see that one on options is selectedvalue, but dropdownlist does not show selected value. why?

change

ViewBag.books = new SelectList(db.books, "id", "name", books.value);

into

ViewBag.books = new SelectList(db.books, "id", "name", book.id); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM