I'm trying to set the selected value of a dropdownlist from the database information.
I load a list from the controller:
public void cargarMotivosFinalizacion()
{
ViewBag.motivosFinalizacion = db.SFI_AtributosDbSet.Where
(x => x.Tipo_Atributo == "MOTIVO FINALIZACION").OrderBy(x =>
x.Descrip_Atributo).Select(x => new SelectListItem
{
Text = x.Descrip_Atributo,
Value = x.ID_Atributo
}).ToList();
}
Reason_Finalization in the database has the code that should be selected in the dropdown but it does not work. Apparently there are many things that I am doing wrong but it is my first project and I do not know what would be the correct way.
The model is:
public class SFI_Salarios
{
[Key]
[Column(Order = 0)]
//[ForeignKey("usuario")]
public string Win_User_ID { get; set; }
public string CUIL { get; set; }
[DisplayFormat(DataFormatString = "{0:N0}", ApplyFormatInEditMode = true)]
public decimal Salario_Base { get; set; }
[Key]
[Column(Order = 3)]
[Required(ErrorMessage = "Fecha de Inicio obligatoria")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public string Fecha_Inicio { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public string Fecha_Fin { get; set; }
public string Motivo_Finalizacion { get; set; }
public string Otros_1 { get; set; }
public virtual SFI_Usuarios usuario { get; set; }
}
View:
@using PagedList;
@using PagedList.Mvc;
@model IPagedList<sfi_1.Models.SFI_Salarios>
@{
ViewBag.Title = "Salarios";
}
<script type="text/javascript" src="@Url.Content("/Scripts/funcionesJS.js")"></script>
<script>
$(document).ready(function ()
{
cambiarFormatoFechaIndex();
})
</script>
@using (Html.BeginForm("Index_Deshabilitados", "SFI_Salarios", FormMethod.Get))
{
IEnumerable<SelectListItem> listaVendedores = (IEnumerable<SelectListItem>)ViewBag.vendedores;
IEnumerable<SelectListItem> listaMotivos = (IEnumerable<SelectListItem>)ViewBag.motivosFinalizacion;
<table class="table">
<tr><th></th></tr>
<tr>
<th><h2>SALARIOS HISTORICOS</h2></th>
</tr>
<tr>
<th>
<div class="row">
<div class="form-inline">
@Html.DropDownList("vendedor", listaVendedores, "Seleccionar Vendedor", htmlAttributes: new { @class = "form-control", @id = "IndexSalarioVendedor" })
<button type="submit" class="btn btn-primary"><span>Buscar</span></button>
@Html.ActionLink("Nuevo Salario", "Create", null, htmlAttributes: new { @class = "btn btn-primary" })
@Html.ActionLink("Limpiar Filtro", "Index", null, htmlAttributes: new { @class = "btn btn-primary" })
@Html.ActionLink("Mostrar Habilitados", "Index", null, htmlAttributes: new { @class = "btn btn-primary" })
</div>
</div>
</th>
</tr>
</table>
<table class="table" id="tablaIndexSalarios">
<tr>
<th>ID Usuario</th>
<th>Nombre Vendedor</th>
<th>Salario</th>
<th>Fecha Inicio</th>
<th>Fecha Fin</th>
<th>Observaciones</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Win_User_ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.usuario.Nombre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Salario_Base)
</td>
<td id="fechaInicio">
@Html.DisplayFor(modelItem => item.Fecha_Inicio)
</td>
<td>
@Html.DisplayFor(modelItem => item.Fecha_Fin)
</td>
<td>
@Html.DropDownListFor(modelItem => item.Motivo_Finalizacion, listaMotivos, "", htmlAttributes: new { @class = "form-control", @id = "IndexSalarioVendedor" })
</td>
<td>
@Html.ActionLink("Editar", "Edit", new { winUser = item.Win_User_ID, fecha = item.Fecha_Inicio }, htmlAttributes: new { @class = "btn btn-primary " })
@Html.ActionLink("Borrar", "Delete", new { winUser = item.Win_User_ID, fecha = item.Fecha_Inicio }, htmlAttributes: new { @class = "btn btn-primary " })
</td>
</tr>
}
</table>
}
@Html.PagedListPager(Model, page => Url.Action("Index", new { page }))
Controller that loads the data:
public ActionResult Index_Deshabilitados(string vendedor, int? page)
{
try
{
if (HttpContext.Application["codigoPerfilUsuario"].ToString() == null)
{
return RedirectToAction("Index", "Home", null);
}
}
catch (Exception)
{
return RedirectToAction("Index", "Home", null);
}
if (!validacionUsuario("Otros_2"))
{
return RedirectToAction("usuarioNoHabilitado");
}
cargarListaVendedores();
cargarMotivosFinalizacion();
return View(db.SFI_Salarios.Where(x =>
(string.IsNullOrEmpty(vendedor) ? true : x.Win_User_ID == vendedor))//.OrderBy(x=> x.usuario.Nombre)
.OrderBy(x => x.usuario.Nombre).ThenBy(x => x.Fecha_Inicio).ThenBy(x => x.Fecha_Fin).ToPagedList(page ?? 1, 20));
}