Receive List in View. Asp.Net Mvc

Asked

Viewed 327 times

0

I’m having trouble with Action Edit.

I have a class called Clientefirm this class has a System collection. For Example: The Customer X uses the systems To, C and F.

My problem is how to present these system in View Edit.

This is my View Get Edit

public ActionResult Editar(int? id)
{
if (id == null)
{
   return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
   ClienteEmpresa clienteEmpresa = db.ClienteEmpresaDb.Find(id);
    if (clienteEmpresa == null)
    {
      return HttpNotFound();
    }
      ViewBag.SistemasComerciais =  clienteEmpresa.TipoDeSistemas.ToList();
      ViewBag.RamoDeAtividade = clienteEmpresa.RamoAtividade;
      return View(clienteEmpresa);
}

I tried to send in a viewBag, but gave this error

The Viewdata item that holds the 'Systemsmercial' key is of the type 'System.Collections.Generic.List`1[[Smc.Dominio.Model.Tipodesystems, Smc.Dominio, Version=1.0.0.0, Culture=neutral, Publickeytoken=null]', but it needs to be 'Ienumerable' type'.

This is the html that gets this list.

<div class="form-group" style="margin-left:-34px; margin-top:17px;margin-bottom:11px;">
<label for="vat" class=" form-control-label">Tipo De Sistema</label>
@Html.DropDownList("SistemasComerciais", null, htmlAttributes: new { @class = "standardSelect", multiple = "multiple", placeholder = "Selecione..." })
</div>

This is the code of the Tipodesistemas class:

public class TipoDeSistemas 
{ 
   [Key] 
   public int Id { get; set; } 

   [Required] 
   public string Descricao { get; set; } 

   public string Cod { get; set; } 

   [Required] 
   public DateTime DataCadastro { get; set; } 

   public virtual ICollection<ClienteEmpresa> ClienteEmpresa { get; set; } 
}

Does anyone know how I can show this list of systems, which my client uses?

2 answers

1

The Html.Helper for dropdownlist is:

MvcHtmlString Html.DropDownList(string name, IEnumerable<SelectLestItem> selectList, string optionLabel, object htmlAttributes)

and the guy in the Viewbag is a List You can or send a IEnumerable or popular the dropdown manually.

  • I didn’t understand very well, has how you do this helper there, with this data here. @Html.DropDownList("SistemasComerciais", null, htmlAttributes: new { @class = "standardSelect", multiple = "multiple", placeholder = "Selecione..." })

  • Try passing the data in one IEnumerable instead of a List

1


The error occurs because you are using null where you should pass the object with your items.

Try it like this:

@Html.DropDownList("SistemasComerciais", new SelectList(ViewBag.SistemasComerciais, "Id", "Nome"), 
    htmlAttributes: new { @class = "standardSelect", multiple = "multiple", placeholder = "Selecione..." })

In this example I am inferring that you have a class (Systemsmercial for example) with the properties Id and Name, if the name of the properties are different just replace where it is "Id" and "Nome" by the names you use on your properties.

The first property (in this example "Id") renders the attribute value in HTML and the second property (in this example "Nome") will be rendered as a description:

<select class="standardSelect" id="SistemasComerciais" multiple="multiple" name="SistemasComerciais" placeholder="Selecione...">
   <option value="1">SistemaComercial a</option>
   <option value="2">SistemaComercial b</option>
   <option value="3">SistemaComercial c</option>
</select>

Editing:


Full example:

Model:

public class TipoDeSistemas
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string Descricao { get; set; }
}

Action:

public ActionResult ViewBagComDropdown()
{
    List<TipoDeSistemas> lista = new List<TipoDeSistemas>()
    {
        new TipoDeSistemas { Id = 1, Descricao = "a" },
        new TipoDeSistemas { Id = 2, Descricao = "b" },
        new TipoDeSistemas { Id = 3, Descricao = "c" }
    };
    ViewBag.SistemasComerciais = lista;            
    return View();
}

View:

<div>
    @Html.DropDownList("SistemasComerciais", new SelectList(ViewBag.SistemasComerciais, "Id", "Descricao", "Selecione... "), 
    htmlAttributes: new { @class = "standardSelect", multiple = "multiple" })
</div>

My rendered view:

inserir a descrição da imagem aqui

  • Now gave that message. DataBinding: 'System.Web.Mvc.SelectListItem' não contém uma propriedade com o nome 'Id'.---- But I have this property Id. is Id and Description I use.

  • public class TipoDeSistemas&#xA; {&#xA; [Key]&#xA; public int Id { get; set; }&#xA; [Required]&#xA; public string Descricao { get; set; }&#xA; public string Cod { get; set; }&#xA; [Required]&#xA; public DateTime DataCadastro { get; set; }&#xA; public virtual ICollection<ClienteEmpresa> ClienteEmpresa { get; set; }&#xA; }

  • Ok, I added the class code to the question. Good about the bug DataBinding: 'System.Web.Mvc.SelectListItem' não contém uma propriedade com o nome 'Id'. I advise you to debug your code and check the return of this property after getting return on line ViewBag.SistemasComerciais = clienteEmpresa.TipoDeSistemas.ToList();

  • I quickly made a complete example and added it to you in my reply.

  • Now for sure the/.. Thank you!!...

Browser other questions tagged

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