Passing a viewbag to a @Dropdownlist

2019-09-21 04:50发布

问题:

I'm trying to pass view bag to a DropDownList but is not working.

Dropdownlist Error

view

   <div class="form-group">
            @Html.LabelFor(model => model.BanhoTosaId, "BanhoTosaId", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
               @Html.DropDownList("TransporteId", ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.BanhoTosaId, "", new { @class = "text-danger" })
            </div>
        </div>

Controller

 // GET: AgendaBTs/Create
        public ActionResult Create(int id, int pet)
        {

            ViewBag.BanhoTosaId = new SelectList(db.BanhoTosas, "BanhoTosaId", "Tipo");
            ViewBag.ClienteId = new SelectList(db.Clientes, "ClienteId", "Nome", id);
            ViewBag.PetId = new SelectList(db.Pets, "PetId", "PetNome", pet);
            ViewBag.TransporteId = new SelectList(db.Transportes, "TransporteId", "Tipo");

            if (ViewBag.BanhoTosaId != null && ViewBag.SelectedValue != null)
            {
                BanhoTosa BT = db.BanhoTosas.Find(ViewBag.BanhoTosaId);
                Transporte TS = db.Transportes.Find(ViewBag.TransporteId);
                decimal valorSoma = BT.Valor + TS.Valor;
                ViewBag.Total = valorSoma;
            }
            else
            {
                ViewBag.Total = 0;
            }

            return View();
        }

If I let the dropdown this way @Html.DropDownList("TransporteId",null, htm.... the if in the controller you throw the else method.

How can i do to solve this?

回答1:

you first need to make a list from data you wanna fill list by it like that :

List<Category> CatList = db.Categories.ToList();

and then use a ViewBag :

 ViewBag.CategoriesList = new SelectList(CatList, "CategoryID", "CategoryName");

after that you will going to view and write:

     <div class="form-group" style="margin-left:85px">
                    <div class="col-md-10">
  @Html.DropDownListFor(model => model.CategoryID, ViewBag.CategoriesList as SelectList,
             "Select Category", new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.CategoryID, "", new { @class = "text-danger" })
                  
                    </div>
                </div>

this example from project i worked on it before



回答2:

You need to cast the ViewBag.BanhoTosaId:

 @Html.DropDownList("TransporteId", (List<SelectList>) ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" })

OR

In your Create action replace this:

ViewBag.TransporteId = new SelectList(db.Transportes, "TransporteId", "Tipo");

By

 ViewBag.TransporteId = db.Transportes;

Add then in your razor view:

@Html.DropDownList("TransporteId", new SelectList(ViewBag.TransporteId , "TransporteId", "Tipo"))


回答3:

Replace

 @Html.DropDownList("TransporteId", ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" })

with

@Html.DropDownList("TransporteId", (IEnumerable<SelectListItem>)ViewBag.BanhoTosaId,null, new { @class = "form-control" })