简体   繁体   中英

How to pass this value in c# ASP.NET?

I'm trying to pass the value of a variable to an object.
I want to pass the value of cod passed to MisArticulos.codigo .

[HttpPost]
public ActionResult AgregarArticulos(ArticuloModel articulo)
{
        ArticulosSele MisArticulos = new ArticulosSele();
        List<ArticulosSele> ListaArticulosFactura = new 
        List<ArticulosSele>();
        dbModels dbArtFact = new dbModels();
        ListaArticulosFactura = dbArtFact.ArticulosSele.ToList();

        var Seleccionados = articulo.Articulos.Where(x => x.IsChecked == 
            true).ToList<Articulos>();
        int tamaño = Seleccionados.Count();//Obtenemos el tamaño de la lista
        for (int i= 0;  i< tamaño; i++) {
            //MisArticulos.foliofactura = idfact;
            int cod= (int)Seleccionados[i].codigo;
            //"MisArticulos.codigo" gives me 0, but "cod" has a value.
            MisArticulos.codigo = cod; 
            MisArticulos.nombre = Seleccionados[i].nombre;//the same problem here
        }
}

The issue lies in your for loop. MisArticulos is a single object, so MisArticulos.codigo = cod; is repeatedly writing over the previous value until the end of the for loop, where presumably cod is 0.

You probably meant to have a list instead, like

List<ArticulosSele> MisArticulos = new List<ArticulosSele>();

and then fill it in your for loop, like

for (int i = 0;  i < tamaño; i++) {
    MisArticulos[i].codigo = Seleccionados[i].codigo;    
    MisArticulos[i].nombre = Seleccionados[i].nombre;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM