Answered by:
Add a drop down list for input text field.

Question
-
User-1320276304 posted
I have an input text box called" Priority type" and I want to create a drop-down list as "High", "Low", "Medium". So this value will insert to the database.
Tuesday, July 28, 2020 4:02 PM
Answers
-
User1686398519 posted
Hi lsuru_anu27,
If you make priority_type’s type is enumerate, chang like this:
public partial class Process { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Process() { this.Process_users = new HashSet<Process_users>(); this.Reports = new HashSet<Report>(); this.Steps = new HashSet<Step>(); } public int Process_id { get; set; } public string Name { get; set; } public string Description { get; set; } public string Type { get; set; } public string Owner { get; set; } public string Start_date { get; set; } public string End_date { get; set; } public string Assigned_dept { get; set; } public Priority_type Priority_type { get; set; } public Nullable<bool> Status { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Process_users> Process_users { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Report> Reports { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Step> Steps { get; set; } } public enum Priority_type { High, Medium, Low } }
Or if not use enumerate, set it’s value range in controller.
Model
public class process { [Key] public int Process_id { get; set; } public string Name { get; set; } public string Description { get; set; } public string Type { get; set; } public string Owner { get; set; } public string Start_date { get; set; } public string End_date { get; set; } public string ssigned_dept { get; set; } public string Priority_type { get; set; } public string Status { get; set; } }
View
<div class="form-group"> @Html.LabelFor(model => model.Priority_type, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownListFor(model=>model.Priority_type,new SelectList(ViewBag.priority,"Value","Text")) @Html.ValidationMessageFor(model => model.Priority_type, "", new { @class = "text-danger" }) </div> </div>
Controller
[HttpGet] public ActionResult Create() { List<SelectListItem> priority = new List<SelectListItem>(); priority.Add(new SelectListItem { Text = "High", Value = "High" }); priority.Add(new SelectListItem { Text = "Medium", Value = "Medium" }); priority.Add(new SelectListItem { Text = "Low", Value = "Low" }); ViewBag.priority = priority; return View(); }
Here is the result.
Best regards,
Yihui Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 29, 2020 6:11 AM
All replies
-
User-474980206 posted
Simple tutorial
https://www.tutorialsteacher.com/mvc/htmlhelper-dropdownlist-dropdownlistfor
Hint, you should always use the *for version
Wednesday, July 29, 2020 1:13 AM -
User-1320276304 posted
Thank you for the answer.
But I'm having errors in this autogenarated modal class.
namespace BPMS { using System; using System.Collections.Generic; public partial class Process { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Process() { this.Process_users = new HashSet<Process_users>(); this.Reports = new HashSet<Report>(); this.Steps = new HashSet<Step>(); } public int Process_id { get; set; } public string Name { get; set; } public string Description { get; set; } public string Type { get; set; } public string Owner { get; set; } public string Start_date { get; set; } public string End_date { get; set; } public string Assigned_dept { get; set; } public Priority_type { get; set; } public Nullable<bool> Status { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Process_users> Process_users { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Report> Reports { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Step> Steps { get; set; } } public enum Priority_type { High, Medium, Low } }
Please Help ME!
Wednesday, July 29, 2020 1:53 AM -
User409696431 posted
You haven't provided the name of the Priority_type instance in the model.
From the example link you were given note the enum Gender defined, and the specific instance of Gender, StudentGender in the model, where StudentGender is referenced by the DropDownListFor as the model item, and the values being fetched are "typeof(Gender)".
public class Student { public int StudentId { get; set; } [Display(Name="Name")] public string StudentName { get; set; } public Gender StudentGender { get; set; } } public enum Gender { Male, Female }
@using MyMVCApp.Models @model Student @Html.DropDownListFor(m => m.StudentGender, new SelectList(Enum.GetValues(typeof(Gender))), "Select Gender")
Do the same in your code, specifying the name of the Priority_type in your model, and referencing both that name, and the typeof(Priority_Type) in the DropDownListFor in your view
Wednesday, July 29, 2020 3:39 AM -
User1686398519 posted
Hi lsuru_anu27,
If you make priority_type’s type is enumerate, chang like this:
public partial class Process { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Process() { this.Process_users = new HashSet<Process_users>(); this.Reports = new HashSet<Report>(); this.Steps = new HashSet<Step>(); } public int Process_id { get; set; } public string Name { get; set; } public string Description { get; set; } public string Type { get; set; } public string Owner { get; set; } public string Start_date { get; set; } public string End_date { get; set; } public string Assigned_dept { get; set; } public Priority_type Priority_type { get; set; } public Nullable<bool> Status { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Process_users> Process_users { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Report> Reports { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Step> Steps { get; set; } } public enum Priority_type { High, Medium, Low } }
Or if not use enumerate, set it’s value range in controller.
Model
public class process { [Key] public int Process_id { get; set; } public string Name { get; set; } public string Description { get; set; } public string Type { get; set; } public string Owner { get; set; } public string Start_date { get; set; } public string End_date { get; set; } public string ssigned_dept { get; set; } public string Priority_type { get; set; } public string Status { get; set; } }
View
<div class="form-group"> @Html.LabelFor(model => model.Priority_type, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownListFor(model=>model.Priority_type,new SelectList(ViewBag.priority,"Value","Text")) @Html.ValidationMessageFor(model => model.Priority_type, "", new { @class = "text-danger" }) </div> </div>
Controller
[HttpGet] public ActionResult Create() { List<SelectListItem> priority = new List<SelectListItem>(); priority.Add(new SelectListItem { Text = "High", Value = "High" }); priority.Add(new SelectListItem { Text = "Medium", Value = "Medium" }); priority.Add(new SelectListItem { Text = "Low", Value = "Low" }); ViewBag.priority = priority; return View(); }
Here is the result.
Best regards,
Yihui Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 29, 2020 6:11 AM -
User-1320276304 posted
Thank you so much!
Thursday, July 30, 2020 2:09 AM