Save multiple enum values in db .net core 2.2

I have some trouble with .net core 2.2 code…

I want to make code saves multiple values in db with comma between them.

But the main question is how can I show multiple values and save them ?

Should I use viewbag?

Enum:

public enum WorkType
{
    Internship,
    Full,
    Part,
    Temporary,
    Student,
    NoExperience
}

Controller:

    public async Task<IActionResult> Create(CreateJobInputModel input)
    {
        ViewBag.Categories = this._categoriesService.GetAll()
         .Select(x => new SelectListItem
         {
             Value = x.Id.ToString(),
             Text = x.TitleAndCount,
         });

        var posterId = _userManager.GetUserId(User);
        ViewBag.EmployerCompanies = this._companyService.GetAllCompanies(posterId)
         .Select(x => new SelectListItem
         {
             Value = x.Id.ToString(),
             Text = x.TitleAndCount,
         });

        if (this.ModelState.IsValid)
        {
            var result = await this._jobsService.Create(input.Id, input.CategoryId, input.CompanyId, input.Name, input.About, input.WorkType, input.Description, input.Location, input.Logo, input.MinSalary, input.MaxSalary, input.Adress, input.JobTags, posterId);

            if (result.Success)
            {
                return Redirect($"/identity/account/manage/jobposts");
            }

        }

        return this.View(input);
    }

View:

<select asp-for="WorkType" asp-items="Html.GetEnumSelectList<WorkType>()" class="form-control" , multiple="multiple"></select>

Archive from: https://stackoverflow.com/questions/59012310/save-multiple-enum-values-in-db-net-core-2-2

Leave a Reply

Your email address will not be published. Required fields are marked *