-2

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

Dropdownlist Error

enter image description here

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?

ViKiNG
  • 1,294
  • 2
  • 19
  • 26
caio oliveira
  • 11
  • 1
  • 6
  • What in the world are you trying to do here? What is the point of `if (ViewBag.BanhoTosaId != null && ViewBag.SelectedValue != null)` since you just set them and they can never be `null` –  Mar 12 '18 at 20:48
  • First you need to cast the SelectList in your method - `@Html.DropDownList("TransporteId", (IEnumerable)ViewBag.BanhoTosaId` ... }` but that makes no sense since you trying to bind to `TransporteId`, not `BanhoTosaId`. But then that will not work either (start by reading [this answer](https://stackoverflow.com/questions/37161202/can-the-viewbag-name-be-the-same-as-the-model-property-name-in-a-dropdownlist) to understand. Stop using view bag and use a view model –  Mar 12 '18 at 20:58

3 Answers3

3

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

1

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"))
Hussein Salman
  • 7,806
  • 15
  • 60
  • 98
0

Replace

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

with

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