Add DropDownList value to the database

1

I wanted to put dropdownlist in my view where its List value was for the database, but I know only how to add values to DropDownlist with data from a table, I wanted to make a DropDownlist with three values without having to create a new table and put it in my View Razor.

Here is an example of what I do in my views:

    @model BlogWeb.ViewsModels.VisitaModel

@Html.ValidationMessage("DtIntegracao.Invalido")

@Html.ValidationMessageFor(v => v.Nome)
@Html.LabelFor(v => v.Nome, "Nome:")
@Html.TextBoxFor(a => a.Nome)

@Html.ValidationMessageFor(v => v.Rg)
@Html.LabelFor(v => v.Rg, "Rg:")
@Html.TextBoxFor(a => a.Rg)

@Html.ValidationMessageFor(v => v.DtIntegracao)
@Html.LabelFor(v => v.DtIntegracao, "Data Integração:")
@Html.TextBoxFor(v => v.DtIntegracao, "{0:dd-MM-yyyy}", new { Type = "date" })

@Html.ValidationMessageFor(v => v.ResponsavelId)
@Html.LabelFor(v => v.ResponsavelId, "Responsavel Entrada:")
@Html.DropDownListFor(v => v.ResponsavelId, new SelectList(ViewBag.Usuarios, "Id", "Nome"))
    
asked by anonymous 06.06.2018 / 15:16

1 answer

0

Two options to be made are either manual or enum , below is an example of the two ways

Manual:

Create a select

<select id="Info" name="Info">
    <option value="0">Selecione</option>
    <option value="1">Info 1</option>
    <option value="2">Info 1</option>
    <option value="3">Info 1</option>
</select>

In your model, create a property with the same%% of select

public class VisitaModel
{
    public int Info { get; set; }
}

Ready, when performing a name the Info property will have the value selected.

Enum:

Create a post , the enum attribute is used to define which text will be displayed

public enum MeuEnum
{
    [Display(Name = "Info 1")]
    Info1 = 1,

    [Display(Name = "Info 2")]
    Info2 = 2,

    [Display(Name = "Info 3")]
    Info3 = 4
}

In your model, create a property with type Display

public class VisitaModel
{
    public MeuEnum MeuEnum { get; set; }
}

In your view, use the helper enum

@Html.EnumDropDownListFor(model => model.MeuEnum, "Selecione", new { @class = "form-control" })

Ready, while holding a EnumDropDownListFor , the MyEnum property will have the value selected.

    
06.06.2018 / 15:32