Creating a Dropdownlist

Asked

Viewed 335 times

0

I’m using Asp.net-mvc-5, and wanted to know how to create a Dropdownlist without being in this pattern here:

        <div id="conteudoEscolha">
            @Html.DropDownList("", null, htmlAttributes: new { @class = "form-control", id = "Musico" })
        </div>

I don’t want to use @html.Dropdownlist, because I will feed it using javascript. I may be wrong, but the way I did up there is making a mistake when I walk on the screen inserir a descrição da imagem aqui

I did as told by @Marcovinicius and then displayed the mesnagem down:

inserir a descrição da imagem aqui

  • Ta sim ta ta nameless!!! That’s why I made the mistake

  • Remembering that this does not limit messing with Javascript.

1 answer

1


you cannot pass null

pass

new List<SelectListItem>()

@Html.DropDownList("batata", new List<SelectListItem>(), htmlAttributes: new { @class = "form-control", id = "Musico" })

if you want you can instantiate in your controller this way

var selectList = new List<SelectListItem>();
selectList.Add(new SelectListItem{Text = "Selecione", Value = ""});

ViewBag.SelectList = selectList;

and in the view would be

@Html.DropDownList("batata",  (List<SelectListItem>)ViewBag.SelectList, htmlAttributes: new { @class = "form-control", id = "Musico" })
  • I did as I said, and then appeared the message below the first image (I edited the question). Thanks

  • 1

    I edited use this way

  • The Error Stopped being pointed, but when I run, the application still says it is null or empty: An Exception of type 'System.Argumentexception' occurred in System.Web.Mvc.dll but was not handled in user code Additional information: Value cannot be null or Empty.

  • the dropdown must have a name

  • It worked out, thanks

Browser other questions tagged

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