Generate a select from an array in asp.net MVC?

0

Cordial greeting colleagues, it turns out that I have a string type array called nodes, in which I keep 4 strings, what I'm trying to do is that in the index view where I have a form, generate a select and fill it automatically with the values of this array, I've been hours in this, I can not find a way to do it.

 //Arreglo donde guardaremos los nodos obtenidos en la lista
            string[] nodos = new string[elementNames.Count];


            foreach (var item in elementNames)  //iterate over the list of elements
            {
                string elementName = item.Value;

                nodos[i] = elementName;

                //Contador para aumentar la posicion del arreglo
                i++;

This code is found in a controller called Functions, in the actionresult index.

This is what I have in the view where I want to generate that select, and that the options are generated automatically with the values of that array.

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
    <title>Index</title>
</head>
<body>

        <div class="container">
            <form action="Funciones/nodos" method="post">

                <label>Elija el nodo del que desea obtener parametros</label> <br />
           <select>

           <option value=""></option>



            </select><br />

                <button type="button" class="btn btn-primary">Enviar</button>
            </form>

        </div>

</body>
</html>

Could you give me an idea of how to do it?

    
asked by Kevin Burbano 19.04.2018 в 21:28
source

2 answers

2

You should do something like ...

public ActionResult Index()
{
    // Obtenemos los elementos con los que queremos llenar el select
    ViewBag.Nodos = elementNames.Select(x => x.Value.ToString()).ToArray();
    // Otra lógica y eso...
    return View(); // En caso de que los nodos de arriba no sean tu modelo.
}

And in your view:

// Index.cshtml del controlador Funciones
<!-- ... Otro contenido ... -->
<div class="container">
    <form action="Funciones/nodos" method="post">
        <label>Elija el nodo del que desea obtener parametros</label><br />
        <select>
            <option value=""></option>
            @if (ViewBag.Nodos != null) {
                foreach (string nodo in ViewBag.Nodos) {
                  <option value="@nodo">@nodo</option>
                }
            }
        </select>
        <button type="button" class="btn btn-primary">Enviar</button>
    </form>
</div>
<!-- ... Otro contenido ... -->

Here is a fiddle so you can see it working:)

    
answered by 19.04.2018 / 21:58
source
1

related to your question it would be necessary to review the following changes at source code level:

in the code of your controller:

public ActionResult Index()
{
//Arreglo donde guardaremos los nodos obtenidos en la lista
            string[] nodos = new string[elementNames.Count];


            foreach (var item in elementNames)  //iterate over the list of elements
            {
                string elementName = item.Value;

                nodos[i] = elementName;

                //Contador para aumentar la posicion del arreglo
                i++;
            }

ViewBag.nodo = nodos;

return View();

}

and already in your razor view would be as follows:

@model IEnumerable<dynamic>
@using tuSolucion.TuModelo;

<div class="col-sm-6">
    <label>Nodos</label>
    <select class="form-control" id="tuNodo" name="tuNodo">
        <option value="">Todos</option>
        @foreach (ObjdeModelo item in ViewBag.nodo)
        {          
          <option value="@item.OtroCampoValor">@item.OtroCampoDesc</option>            
        }
    </select>
</div>

I hope you find it useful, greetings.

    
answered by 19.04.2018 в 21:56