Home > Blockchain >  Make a field a dropdown with values EF Core
Make a field a dropdown with values EF Core

Time:11-16

I have a field in the Database like [OrderDay] which is of the type nvarchar(3) I have used the EF core to scaffold the Model, Views and Controller. Now the field allows the users to freely type any values as it is <input>

        <div class="form-group">
            <label class="control-label">Order Day</label>
            <input asp-for="OrderDay" class="form-control" />
            <span asp-validation-for="OrderDay" class="text-danger"></span>
        </div>

Below is the Model Cass

public partial class Setting
{
    public string EndTime { get; set; }
    public string StartTime { get; set; }
    public string OrderDay { get; set; }
    public int SettingsId { get; set; }
}

Is it possible I can change the <input> to be a dropdown and build the values of the dropdown with in the application like it to hold like ('MON','TUE','WED','THU','FRI'). I able to find binding the dropdown with the Database but in the DB I dont have the table to hold this data instead I need to handle with in the application. Is this doable, any help is greatly appreciated

CodePudding user response:

In your form:

<select asp-for="OrderDay" class="form-control"></select>

Your model class:

public partial class Setting
{
    public string EndTime { get; set; }
    public string StartTime { get; set; }
    public List<string> OrderDay { get; } = new List<string> {
         "Mon", "Tue", "Wed", "Thu", "Fri" 
        } ;
    public int SettingsId { get; set; }
}
  • Related