How to assign Combobox value to variable?

Asked

Viewed 260 times

1

I am customizing the user registration part using the Entity Framework Identity tool.

So on the registration page, I added a ComboBox (Select in HTML):

inserir a descrição da imagem aqui

Part of my View:

 <div class="form-group">
    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.Departamento, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">           
        <select class="form-control" id="select">
            <option>Administração</option>
            <option>Produção</option>
            <option>Vendas</option>
            <option>Comercial</option>
            <option>Compras</option>
            <option>Qualidade SGQ</option>
            <option>Qualidade Inspeção</option>
            <option>Engenharia</option>
            <option>Serviços</option>
            <option>Desenvolvimento</option>
            <option>Marketing</option>
            <option>T.I</option>
        </select>
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.Telefone, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Telefone, new { @class = "form-control" })
    </div>
</div>

And I defined the "Department" field as required (mandatory):

 public class RegisterViewModel
{
    [Required]
    [Display(Name = "Usuario")]
    public string Usuario { get; set;}

    [Required]
    [Display(Name = "Nome")]
    public string Nome { get; set; }

    [Required]
    [Display(Name = "Sobrenome")]
    public string Sobrenome { get; set; }

    [Required]
    [Display(Name = "Departamento")]
    public string Departamento { get; set; }

    [Display(Name = "Telefone")]
    public string Telefone { get; set; }

    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

However, when trying to register the new user, even if I select the department is as if it is blank:

inserir a descrição da imagem aqui

1 answer

0


Failed to complete the property value tag <option>:

<select id="dropdowntipo">
   <option value="Exemplo1">Exemplo1</option>
   <option value="Exemplo2">Exemplo2</option>
   <option value="Exemplo3">Exemplo3</option>
</select>

As you are already using Razor can use the @Html.DropDownListFor

@Html.DropDownListFor(x => x.Tipo, new List<SelectListItem>
{
                    new SelectListItem() {Text = "Exemplo1", Value="Exemplo1"},
                    new SelectListItem() {Text = "Exemplo2", Value="Exemplo2"},
                    new SelectListItem() {Text = "Exemplo3", Value="Exemplo3"}
}) 
  • Friend, what’s the difference between the two?

  • 1

    None because the code that will be sent to the browser will be the same, the question is to follow the pattern, in this case it is developing with the Razor other tags, so in my opinion better continue following.

Browser other questions tagged

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