我试图选择一个项目将它的价格写入输入中,这样我就可以计算税收和东西了。
我设法填充DropDownList,但无法弄清楚如何获得它的价值。
到目前为止我的代码:
查看模型
public List<Producto> Productos;
[Display(Name = "Producto")]
public int ProductoId { get; set; }
public IEnumerable<SelectListItem> ProductosItems
{
get
{
var todosProductos = Productos.Select(f => new SelectListItem
{
Value = f.ID.ToString(),
Text = f.Nombre + " - S/" + f.Pecio
});
return todosProductos;
}
}
查看
<div class="form-group">
@Html.LabelFor(m=>m.ProductoId, new { @class="col-md-2 control-label" })
@Html.DropDownListFor(
m => m.ProductoId,
Model.ProductosItems,
"-- Seleccionar --",
new { @class = "form-control", @onchange="IDproducto();" }
)
@Html.ValidationMessageFor(m => m.ProductoId)
</div>
控制器
public IActionResult Create()
{
VentaViewModel model = new VentaViewModel()
{
Clientes = _context.Cliente.ToList(),
Productos = _context.Producto.ToList()
};
return View(model);
}
答案 0 :(得分:1)
使用jQuery监听下拉列表的更改,并在用户更改后获取当前选定的值:
<script>
$(document).ready(function() {
// use @Html.IdFor so this does not break
// if you rename the ProductoId property in the ViewModel
var dropdown = $('#@Html.IdFor(m => m.ProductoId)');
dropdown.on('change', function () {
// this gets the "value" attribute of the selected option
// if you need the text, use .text() instead of .val()
var selected = $('option:selected', dropdown).val();
// work with selected value ...
});
});
</script>