I’m new to dev so hope you can help.
I’m having issues saving the selected item on a Enum dropdown list to the database.
The screen shows the dropdown list, but when you save the workout the list saves the first value to the database to the selected one AMRAP = 0
Enum Example:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Xml.Linq;
namespace MyFitnessApplication.Enums
{
public enum WorkoutTypeEnum
{
AMRAP = 0,
Tabata = 1,
METCON = 2,
WEIGHTLIFTING = 3
}
}
Controller Example:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,WorkoutDate,WorkoutName,WorkoutTypeEnum,WorkoutDetails")] ManageWorkouts manageWorkouts)
{
if (ModelState.IsValid)
{
db.ManageWorkouts.Add(manageWorkouts);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(manageWorkouts);
}
Model Example:
[Required]
[DisplayName("Workout Type")]
//public int WorkoutType { get; set; }
//[EnumDataType(typeof(Enums.WorkoutTypeEnum))]
public Enums.WorkoutTypeEnum WorkoutType { get; set; }
View Example:
<div class="form-group">
@Html.LabelFor(model => model.WorkoutType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(model => model.WorkoutType, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.WorkoutType, "", new { @class = "text-danger" })
</div>
</div>
New contributor
Coenraad Rossouw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.