Treat and Save List in Asp.Net MVC Controller

Asked

Viewed 72 times

0

I have a Customer Class, which uses 1 or several systems.

    public class ClienteEmpresa : Pessoa
    {
    public virtual ICollection<TipoDeSistemas> TipoDeSistemas { get; set; }
    }

In my Action Get Create I search the list in the bank and send to the view

   public ActionResult _frmCadastroCliente()
        {
            ViewBag.SistemaComercial = new SelectList(db.TipoDeSistemaDb, "Id", "Descricao");
            return PartialView();
        }

In my View I have a select that receives this list.

<div class="row">
    <div class="col-md-12" style="margin-left:10px; min-width:100%;">
        <div class="form-group" style="margin-left:-7px; margin-top:17px;margin-bottom:11px;">
            <label for="vat" class=" form-control-label">Tipo De Sistema</label>
                @Html.DropDownList("SistemaComercial", null, htmlAttributes: new { @class = "standardSelect", multiple = "multiple" })
        </div>
    </div>
</div>

In my Action Post Create I get an array of Ids.

public ActionResult Create(ClienteEmpresaViewModel clienteEmpresaviewmodel, int[] SistemaComercial)
{
  //Aqui que eu quero saber como tratar e salvar essa lista.
  //Eu tentei Assim
   foreach (var sistema in SistemaComercial)
    {
     //Mas não sei como salvar.
    }
}

Could someone give me a hint on how to assign this list to my Client?

1 answer

0


I managed to solve it, as follows. (if anyone else goes through the msm thing).

public ActionResult Create(ClienteEmpresaViewModel clienteEmpresaviewmodel,List<int> SistemaComercial)
    {
      ClienteEmpresa cl = new ClienteEmpresa();
       foreach (var sistema in SistemasComerciais)
       {
        TipoDeSistemas ts = db.TipoDeSistemaDb.Find(sistema);
        cl.TipoDeSistemas.Add(ts);
       }
    }

Browser other questions tagged

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