Mostrar Select (html) con datos a partir de un DataTable en C# & Razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.UI.WebControls; | |
namespace VisibilidadWeb.Models.Methods { | |
public class _Convert { | |
/** | |
* <summary>Convierte un System.Data.DataTable a un System.Web.Mvc.SelectList</summary> | |
* <param name="table">DataTable a convertir en SelectList</param> | |
* <param name="valueField">Columna a tomar como clave: identificador, clave, ID...</param> | |
* <param name="textField">Columna a mostrar: Nombre, descripción...</param> | |
* <returns>System.Web.Mvc.SelectList</returns> | |
*/ | |
public static SelectList DataTableToList(DataTable table, string valueField, string textField) { | |
List<ListItem> list = new List<ListItem>(); | |
foreach (DataRow row in table.Rows) { | |
list.Add(new ListItem() { | |
Text = row[valueField].ToString(), | |
Value = row[textField].ToString() | |
}); | |
} | |
return new SelectList(list, "Value", "Text"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class=""> | |
@Html.LabelFor(model => model.griv_idgrupoinv, htmlAttributes: new { @class = "" }) | |
@Html.DropDownList("griv_idgrupoinv", new SelectList(ViewBag.GrupoInv, "Text", "Value"), new { @class = "form-control" }) | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
using System.Data; | |
using VisibilidadWeb.Models.BD; | |
namespace VisibilidadWeb.Models { | |
public class GrupoInvestigacionModel { | |
[DataType(DataType.MultilineText)] | |
[Display(Name = "ID Grupo de investigacion")] | |
public int griv_idgrupoinv { get; set; } | |
[Required] | |
[DataType(DataType.DateTime)] | |
[Display(Name = "Fecha de creacion")] | |
public string griv_fechacreacion { get; set; } | |
[Required] | |
[DataType(DataType.Text)] | |
[Display(Name = "Nombre")] | |
public string griv_nombre { get; set; } | |
[Required] | |
[DataType(DataType.MultilineText)] | |
[Display(Name = "Descripcion")] | |
public string griv_descripcion { get; set; } | |
[Required] | |
[DataType(DataType.Text)] | |
[Display(Name = "ID User director")] | |
public long user_director { get; set; } | |
Conexion conexion = new Conexion(); | |
public GrupoInvestigacionModel() { } | |
/** | |
Función utilizada SemilleroController.cs | |
*/ | |
public DataTable get_grupo_investigacion() { | |
return conexion.realizarConsulta("PR_SELECT_GRUPO_INVESTIGACION ", "CR_RESULT ", null); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Data; | |
using System.Web.Mvc; | |
using VisibilidadWeb.Models; | |
using VisibilidadWeb.Models.Methods; | |
namespace VisibilidadWeb.Controllers { | |
public class SemilleroController : Controller { | |
SemilleroModel _semillero = new SemilleroModel(); | |
GrupoInvestigacionModel _grinv = new GrupoInvestigacionModel(); | |
#region crear semillero | |
[HttpGet] | |
public ActionResult Crear() { | |
ViewBag.GrupoInv = _Convert.DataTableToList(_grinv.get_grupo_investigacion(), "smlr_idsemillero", "smlr_nombre"); | |
return View(); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment