@Html.Dropdownlistfor Default item at first list values

Asked

Viewed 320 times

1

Good afternoon, gentlemen.

I use DropDownListFor To load lists, example load all my clients to be selected. However, I need that if a record does not appear in the listing have the option to add a new item.

I can write the code, however the problem is that the item to add a new record is at the end of my list, I would like it to be the second item. The first item is the select, second new and the others who are registered.

@Html.DropDownListFor(model => model.ClienteId, new SelectList(@ViewBag.Clientes, "Id", "Nome") ,"Selecione")

JS:

 $('#ClienteId').append($('<option>', { text: "Novo", value: 0, selected: false }));

2 answers

0

It would be something like that:

$('#ClienteId').append($('<option>', { text: "Novo", value: 0, selected: false }));
var options = $('#ClienteId option');
$(options[options.size() - 1]).insertAfter($(options)[1]));
  • The insertAfter command was not recognized, for min.

  • I gave an update on the reply. He came to see?

  • Oops, I hadn’t seen it. So, I tested code and no error occurred, but it didn’t work. "New"

  • I think it will now appear. I just tested it here on a screen of mine. The first version was really wrong.

  • So para min didn’t work. The order shown is the order the data were entered.

0

First of all, I don’t think this is the ideal way to specify that a new record will be inserted, the Dropdownlist should have only those items that can be used to fill in the model and that optional field that is displayed when no item is selected.

You could for example have a checkbox that when marked would enable the Dropdownlist and when unchecked would display some other option add a new client.

But Emfim, you can insert this "New" field into Dropdownlist by the server side as follows:

(For those who will judge me, I know it is an ugly solution, but there is nothing else that can be done to answer the OP question by the server side).

public ActionResult Index()
{
    // Vamos supor que os dados dos clientes vieram do banco de dados
    var clientes = new List<Cliente>
    {
        new Cliente { Id = 1, Nome = "Foo" },
        new Cliente { Id = 2, Nome = "Bar" }
    };

    // Com a coleção de Clientes em mãos, insira um novo Cliente no primeiro índice e atribua "Novo" à propriedade que será exibida na DropDownList
    clientes.Insert(0, new Cliente { Nome = "Novo" });

    // E agora transforme a coleção de Clientes em uma SelectList
    ViewBag.Clientes = new SelectList(clientes, "Id", "Nome");

    return View();
}
  • And what would be a good solution, speaking of usability? I see some systems that are high working this way and I also liked the usability of it.

  • @Zica What does the system you are working on do in this case? When the user selects the "New"?

  • It opens a form within the modal dialog to register a new client. After saving the new client, the Dropdownlistfor is automatically updated and the registered client is selected. All this is already working, I just wanted to get better.

  • I would do as follows, display the Dropdownlist only with customers, and next to it a button to add a new client and put the modal dialog to open when there was a click on that button

Browser other questions tagged

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