Hello, good night!
I’m not sure I understand what you need, but I think this template below might help you get an idea of how to solve your issue:
Note: This is a very didactic example and mainly in the controller I added options by hand and not searching from a database to make it easier to explain. However, this solution can be improved.
Model:
public IEnumerable<SelectListItem> ListaDeDescontoTratada { get; set; }
public string DescontoEscolhido { get; set; }
View:
@Html.DropDownListFor(x => x.DescontoEscolhido, Model.ListaDeDescontoTratada, new { @id = "ListaDeDescontos", size = 1, @class = "form-control", MultiSelectList = "false" })
Controller:
public IActionResult LocalOndeTemDesconto()
{
List<SelectListItem> descontosListados = new List<SelectListItem>();
string descontoEscolhido = string.Empty;
SelectListItem Item1 = new SelectListItem()
{
Text = "Desconto 1: 10%",
Value = "D1"
};
SelectListItem Item2 = new SelectListItem()
{
Text = "Desconto 2: 20%",
Value = "D2"
};
SelectListItem Item3 = new SelectListItem()
{
Text = "Desconto 3: 30%",
Value = "D3"
};
descontosListados.Add(Item1);
descontosListados.Add(Item2);
descontosListados.Add(Item3);
LocalOndeTemDescontoModel localOndeTemDescontoModel = new LocalOndeTemDescontoModel()
{
DescontoEscolhido = descontoEscolhido,
DescontosListados = descontosListados
};
return View(localOndeTemDescontoModel);
}
I think the legal way to do this is to create a backend property that already returns formatted.
– rLinhares
@rLinhares, I didn’t understand. How would you do it?
– pnet
If I create another Model for this, I can use this Model in the View along with the other view?
– pnet
AzureDiscountGroup
is aIEnumerable
?– Leandro Angelo
is a
public virtual AzureDiscountGroup AzureDiscountGroup { get; set; }
– pnet
That example help you? Select list vc can assemble by reading your property information.
– George Wurthmann
@pnet that >
public virtual AzureDiscountGroup AzureDiscountGroup { get; set; }
is not a list, how do you want to turn into a Dropdownlistfor<>? You can try to improve the question?– George Wurthmann