How to use Dropdownlistfor

Asked

Viewed 32 times

0

In an archive .cshtml need to put a Dropdownlistfor<>. It would be populated by a virtual property, which is a foreign key in this Model. It would be this on .cshtml:

<div class="form-group">
        @Html.LabelFor(model => model.AzureDiscountGroupId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.AzureDiscountGroup????)
        </div>
    </div>

I need to show the description and the discount value, but no change, filter by the discount ID. How would you do this?

  • I think the legal way to do this is to create a backend property that already returns formatted.

  • @rLinhares, I didn’t understand. How would you do it?

  • If I create another Model for this, I can use this Model in the View along with the other view?

  • AzureDiscountGroup is a IEnumerable?

  • is a public virtual AzureDiscountGroup AzureDiscountGroup { get; set; }

  • That example help you? Select list vc can assemble by reading your property information.

  • @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?

Show 2 more comments

1 answer

3

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);
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.