Dropdownlistfor creation with typed view

Asked

Viewed 79 times

0

Good afternoon, everyone,

I’m starting my studies in the art of ASP.NET C#.
I’m having trouble creating a Dropdownlistfor.

Like I’m doing:
- I’m using ADO.NET
- I have my model Equipment
- I have a viewmodel Equipmentcompany, just to list all companies(id and description).
- in my Equipment controller, in the Insert action, I create the following:

  ViewBag.ListaEmpresa = new SelectList(
                                        new EmpresaDAO().ListaEmpresaDropDown().ToList(),
                                                   "EmpresaId",
                                                   "CodEmpresa"
                                                );


- These are some of the ways I’ve tried to make ddl creation, but without success:

@model Objects.ValueObjects.EquipamentoVO

@Html.DropDownListFor(m => m.EmpresaId, new SelectList((ViewBag.ListaEmpresa) as SelectList, "EmpresaId", "CodEmpresa"), "Selecione uma empresa")
@Html.DropDownListFor(m => m.EmpresaId, new SelectList(ViewBag.ListaEmpresa, "EmpresaId", "CodEmpresa"), "Selecione uma empresa")
@Html.DropDownListFor(m => m.EmpresaId, new SelectList(@ViewBag.ListaEmpresa,"ListaEmpresa", "CodEmpresa"), "Selecione uma empresa")
<br>

Would anyone know where I’m going wrong? or give me an idea of how to create a DDL with a typed view?

  • Explain the error better, maybe describe better what happens when you rotate or pass the text that comes in error.

  • This is the message that occurs Additional information: Databinding: 'System.Web.Mvc.Selectlistitem' does not contain a property named 'Empresaid'. In one of the ways I did, he brought the correct dropdown number, only did not bring records, appeared the name of the component

  • put . Value at the end and try again

  • Which part I place . Value?

  • In the part that should appear the records , where you said the names of the components were appearing

  • this is the code that shows the name of the object @Html.Dropdownlistfor(m => m.Empresaid, new Selectlist(Viewbag.Listaempresa, "Codempresa"), "Select a company")

  • @Html.Dropdownlistfor(m => m.Empresaid, new Selectlist(Viewbag.ListaEmpresa.Value, "Codempresa"), "Select a company")

  • Additional information: 'System.Web.Mvc.Selectlist' does not contain a definition for 'Value''

  • Mmm, I’m pretty sure the problem is in this Viewbag.Listcompany, try to find in it what you want to show and put . NameName to not return the object name. Replace part . Value of my previous message with this. Nameannfo you want to display and see if return the properties you want

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.