Here is the Razor code
@Html.DropDownList("ddl", Model.estados.Select(item => new SelectListItem
{
Value = item.Id_Estado.ToString(),
Text = item.Nombre_Estado,
Selected = "select" == item.Id_Estado.ToString()
}), new { @class = "form-select", aria_label="Default select example" }
)
Here is the view model, it’s an IEnumerable
:
public class ViewModel
{
public UsuariosViewModel usuario { get; set; }
public IEnumerable<TiposUsuariosViewModel> tiposUsuarios { get; set; }
public IEnumerable<EstadosViewModel> estados { get; set; }
}
2
Answers
change the model
in your action add estado items to a view model
and view
but it is better to use select, since it is automatically selects item from list
I am assuming the you want to send the value of the selected option to your
Controller
method. Now since you have not shown yourController
method, I will give a basic example usingAJAX
andJquery
:First give an
id
to your drop down list:You can have a button which will invoke the event or whatever event you are using, you can do that. I am using a button event here:
Then you can use AJAX to send it to your
Controller
method and get a response back:And finally your
Controller
method will be: