Skip to content

Instantly share code, notes, and snippets.

@juliandavidmr
Created January 8, 2017 21:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliandavidmr/c76cc5053da48b9f04aae52ff9e46f30 to your computer and use it in GitHub Desktop.
Save juliandavidmr/c76cc5053da48b9f04aae52ff9e46f30 to your computer and use it in GitHub Desktop.
Mostrar Select (html) con datos a partir de un DataTable en C# & Razor
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");
}
}
}
<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>
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);
}
}
}
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