Hello friends,
I am developing an ASP NET CORE solution with C # and SQL Server.
The problem I have is that I require that when the page loads, the elements of the two lists are selected, with default values for each one previously by the user.
I already get the value from the database and pass it to the view through the ViewBag.ClaveProdServ and assign it to the IdPredeterminado variable
For the dropdowlistFor "SAT Clave", I fill it with the variable Claves of type List <SelectListItem> ();
With the value of the variable, DefaultID, how can I select the item from the list?
@model Modelo.Facturacion.Productos
@{
Layout = "_FormLayout";
ViewData["Title"] = "Agregar Producto/Servicio";
ViewData["Modulo"] = "Producto";
ViewBag.IncluyeUrlEnForm = true;
IEnumerable<SelectListItem> unidades = new List<SelectListItem>();
IEnumerable<SelectListItem> claves = new List<SelectListItem>();
IEnumerable<SelectListItem> impuestos = new List<SelectListItem>();
string Existe = null;
int IdPredeterminado = 0;
if (ViewBag.unidades != null)
{
unidades = (List<SelectListItem>)ViewBag.unidades;
}
if (ViewBag.ClaveProdServ != null)
{
IdPredeterminado = ViewBag.ClaveProdServ;
}
if (ViewBag.Claves != null)
{
claves = (List<SelectListItem>)ViewBag.Claves;
}
if (ViewBag.Existe != null)
{
Existe = ViewBag.Existe;
}
if (ViewBag.Impuestos != null)
{
impuestos = ViewBag.Impuestos;
}
}
<div class="row">
<div class="col-md-4">
<form asp-action="Registrar">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<input hidden value="1" id="ID_USUARIOS" name="ID_USUARIOS" />
</div>
@if (Existe == "SI")
{
<font color="red" size=4>Ya existe un producto con el mismo nombre</font>
}
<div class="form-group">
<font color="red" size=5>*</font>
<label asp-for="Nombre" class="control-label"></label>
<input asp-for="Nombre" class="form-control" maxlength="1000" />
<span asp-validation-for="Nombre" class="text-danger"></span>
</div>
<div class="form-group">
<font color="red" size=5>*</font> <label asp-for="Clave" class="control-label">Clave</label>
@* @Html.DropDownListFor(u => u.Clave, claves, "-- Seleccione Clave Producto/Servicio --", new { @class = "form-control" })*@
<input asp-for="Clave" class="form-control" maxlength="10" />
<span asp-validation-for="Clave" class="text-danger"></span>
</div>
@*@*<div class="form-group">
<font color="red" size=5>*</font> <label asp-for="IdSATProductoServicio" class="control-label">Clave SAT</label>
@Html.DropDownListFor(u => u.Clave, claves, "-- Seleccione Clave Producto/Servicio --", new { @class = "form-control" })
<span asp-validation-for="IdSATProductoServicio" class="text-danger"></span>
</div>*@
<div class="form-group">
<font color="red" size=5>*</font> <label asp-for="IdSATProductoServicio" class="control-label">Clave SAT</label>
@Html.DropDownListFor(u => u.IdSATProductoServicio, claves, new { @class = "form-control" })
<span asp-validation-for="IdSATProductoServicio" class="text-danger"></span>
</div>
<div class="form-group">
<font color="red" size=5>*</font><label asp-for="UnidadMedida" class="control-label"></label>
<input asp-for="UnidadMedida" class="form-control" maxlength="20" />
<span asp-validation-for="UnidadMedida" class="text-danger"></span>
</div>
<div class="form-group">
<font color="red" size=5>*</font> <label asp-for="IdSATUnidadMedida" class="control-label"></label>
@Html.DropDownListFor(u => u.IdSATUnidadMedida, unidades, new { @class = "form-control" })
<span asp-validation-for="IdSATUnidadMedida" class="text-danger"></span>
</div>
<div class="form-group">
<font color="red" size=5>*</font> <label asp-for="PrecioUnitario" class="control-label"></label>
<input type="number" value="0" asp-for="PrecioUnitario" class="form-control " />
<span asp-validation-for="PrecioUnitario" class="text-danger"></span>
</div>
<font color="red" size=5>*</font><font color="black" size=3>=Dato Requerido</font>
<br />
<hr style="height:1px;border:none;color:#333;background-color:#333;" />
<font color="black" size=3>Impuestos</font>
<hr style="height:1px;border:none;color:#333;background-color:#333;" />
@foreach (var item in impuestos)
{
<br />
<tr>
<td>
@{
var nombre = "Impuesto"; /*+ @item.Value;*/
}
@*@Html.CheckBox(item.Text)*@
<input name=@nombre type="checkbox" value="@item.Value" /> @item.Text<br />
@*<input type="checkbox" id="chk" value="second_checkbox"> <label for="cbox2">Este es mi segundo checkbox</label>*@
@*<label>
@item.Text
</label>*@
</td>
</tr>
}
</form>
</div>
</div>
@section Styles {
<link rel="stylesheet" href="~/lib/select2/css/select2.css" />
}
@section Scripts {
<script type="text/javascript" src="~/lib/select2/js/select2.js"></script>
<script>
$(document).ready(function () {
$('select').select2();
});
var form = Vue.component('test', {
template: '#test',
data: {
Email: 'sada'
},
methods: {
validate: function () {
var self = this;
self.$validator.validateAll().then(function (resolve) {
if (resolve) {
}
})
}
}
});
</script>
}
My Class
namespace Modelo.Catalogos.Sat
{
[Table("SATProductoServicio")]
public class SATProductoServicio
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID_PRODUCTO_SERVICIO { get; set; }
[Column(TypeName = "varchar(9)")]
[Required]
public String CLAVE { get; set; }
[Column(TypeName = "varchar(150)")]
[Required]
public String DESCRIPCION { get; set; }
[Required]
[Column(TypeName = "datetime")]
public DateTime FECHA_INICIO_VIGENCIA { get; set; }
[Column(TypeName = "datetime")]
public DateTime? FECHA_FIN_VIGENCIA { get; set; }
[Column(TypeName = "varchar(10)")]
[Required]
public String INCLUIR_IVA_TRASLADADO { get; set; }
[Column(TypeName = "varchar(9)")]
[Required]
public String INCLUIR_IEPS_TRASLADADO { get; set; }
[Column(TypeName = "varchar(2)")]
public String COMPLEMENTO_QUE_DEBE_INCLUIR { get; set; }
}
}