How to update Enum value in ASP.NET Core MVC?

I am trying to update Enum value which display Status with three value, Open, OnHold, Close

public enum TicketStatus
{
    Otvoren = 1,
    NaCekanju = 2,
    Zatvoren = 3
}

I assign this value to my Model field and give the default value of Open

public string Status { get; set; } = TicketStatus.Otvoren.ToString();

Right now the problem start when I want to update this value, and change from Open to OnHold or Close

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Upsert(TicketVM ticketVM)
{
    var users = _unitOfwork.ApplicationUser.GetAll(x => x.Id == x.UserName);
    var userName = User.FindFirstValue(ClaimTypes.Email);
    var user = HttpContext.User.Identity.Name;

    if (ModelState.IsValid)
    {
        if (ticketVM.Ticket.Id == 0)
        {
            ticketVM.Ticket.ApplicationUser = _db.ApplicationUsers.FirstOrDefault(u => u.Email == userName);
            ticketVM.Ticket.Status = TicketStatus.Otvoren.ToString();
            _unitOfwork.Ticket.Add(ticketVM.Ticket);
        }
        else
        {
            ticketVM.Ticket.ApplicationUser = _db.ApplicationUsers.FirstOrDefault(u => u.Email == userName);
            
            _unitOfwork.Ticket.Update(ticketVM.Ticket);
        }
        _unitOfwork.Save();
        return RedirectToAction(nameof(Index));
    }
    return View(ticketVM);
}

Here is the Upsert method where Status should be updated. So far what I try is this:

Method 1

if (ticketVM.Ticket.Status == "Otvoren")
{
    ticketVM.Ticket.Status = TicketStatus.NaCekanju.ToString();
    ticketVM.Ticket.Status = TicketStatus.Zatvoren.ToString();
}
else if (ticketVM.Ticket.Status == "NaCekanju")
{
    ticketVM.Ticket.Status = TicketStatus.Otvoren.ToString();
    ticketVM.Ticket.Status = TicketStatus.Zatvoren.ToString();
}
else if(ticketVM.Ticket.Status == "Zatvoren")
{
    ticketVM.Ticket.Status = TicketStatus.Otvoren.ToString();
    ticketVM.Ticket.Status = TicketStatus.NaCekanju.ToString();
}

Method 2

if (status == "Otvoren")
{
    status = TicketStatus.Otvoren.ToString();
}
else if (status == "NaCekanju")
{
    status = TicketStatus.NaCekanju.ToString();
}
else if (status == "Zatvoren")
{
    status = TicketStatus.Zatvoren.ToString();
}

Method 3

switch (status)
{
    case "Otvoren":
        TicketStatus.Otvoren.ToString();
        break;
    case "NaCekanju":
        TicketStatus.NaCekanju.ToString();
        break;
    case "Zatvoren":
        TicketStatus.Zatvoren.ToString();
        break;
}

And here is my HTML dropdown

<div class="form-group row">
    <div class="col-4">
        <label>Status Tiketa</label>
    </div>
    <div class="col-8">
        <select asp-for="@Model.Ticket" asp-items="Html.GetEnumSelectList<VmSTicketing.Models.Enum.TicketStatus>()" class="form-control"></select>
    </div>
</div>
  • Method 1 doesn’t update status at all, and value is always default as it is Open
  • Method 2 same as Method 1
  • Method 3 doesn’t work at all too

My question is: How can I update value from enum dropdown menu using ?

Answer

Your HTML is not sending any value for ticket status, and the model binder is always using the default initialized value.

Change the asp-for in select tag from @Model.Ticket to @Model.Ticket.Status

<select asp-for="@Model.Ticket.Status" asp-items="Html.GetEnumSelectList<VmSTicketing.Models.Enum.TicketStatus>()" class="form-control"></select>

This will give you the string representation of the int value you set to the enum members. You can get the enum string from that and set it to the ticket status, like –

ticketVM.Ticket.Status = ((TicketStatus)Convert.ToInt32(ticketVM.Ticket.Status)).ToString();

An alternative approach:

For create and edit operation, in your controller you have GET methods that returns the view. Create a SelectList with your enum member names and add it to the ViewBag

public IActionResult Create()
{
    ViewBag.StatusList = new SelectList(Enum.GetNames(typeof(TicketStatus)));
    return View();
}

and in your HTML change the select tag as –

<select asp-for="@Model.Ticket.Status" asp-items="ViewBag.StatusList" class="form-control"></select>

Now you will get the enum member name selected in the dropdown as the value for ticketVM.Ticket.Status in the controller and you don’t have to do anything to set this value.

Leave a Reply

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