How to assign value from ComboBox to variable?

1

I'm customizing the user registry part, using the Entity Framework's Identity Tool.

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

PartofmyView:

<divclass="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 set the "Department" field as required (required):

 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 when I select the department, it is as if it were blank:

    
asked by anonymous 16.10.2016 / 16:51

1 answer

0

Failed to fill property value of tag <option> :

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

Since you are already using Razor you can use @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"}
}) 
    
16.10.2016 / 17:09