none
MVC Dropdowns, can't get a dropdown in my form

    Pertanyaan

  • My VolunteerInfo.cs Model

               
    public class VolunteerInfo
        {
            [Key]
            publicint VolunteerId { get; set; }
     
            [Required(ErrorMessage = "Name is required")]
            [DisplayName("Name*")]
            publicstring Name { get; set; }
     
            [Required(ErrorMessage = "Birth day is required")]
            [DisplayName("Date of Birth*")]
            publicDateTime BirthdayDateTime { get; set; }
     
            [Required(ErrorMessage = "Phone number is required")]
            [DisplayName("Phone Number*")]
            publiclong PhoneNumber { get; set; }
     
            publicstring Address { get; set; }
     
            [Required(ErrorMessage = "Email is required")]
            [DisplayName("Email*")]
            publicstring EmailAddress { get; set; }
     
            [Required(ErrorMessage = "Occasion is required")]
            [DisplayName("Occasion*")]
            publicOccasion Occasions { get; set; }
     
            publicvirtualIEnumerable<VolunteerInfo> VolunteerInfos { get; set; }
        }
    


    My Occasion.cs Model

     public class Occasion
        {
            [Key]
            public int Id { get; set; }
    
            public List<SelectListItem> OccasionForDonation { get; set; }
    
    
            
        }

    My VolunteerInfoController.cs

    public class VolunteerInfoController : Controller
        {
    
            private readonly ApplicationDbContext _context;
            public VolunteerInfoController()
            {
                _context = new ApplicationDbContext();
            }
    
            // GET: VolunteerInfo
            public ActionResult AddVolunteer()
            {
                return View();
                
            }
    
            [HttpPost]
            public ActionResult AddVolunteer(VolunteerInfo viewModel)
            {
                if (!ModelState.IsValid)
                {
                    return View("AddVolunteer", viewModel);
                }
    
                var volunteer = new VolunteerInfo()
                {
                    Name = viewModel.Name,
                    BirthdayDateTime = viewModel.BirthdayDateTime,
                    Address = viewModel.Address,
                    PhoneNumber = viewModel.PhoneNumber,
                    EmailAddress = viewModel.EmailAddress,
                    //Occasions = ??????? //Don't know what to put here
    
                };
    
    
                _context.VolunteerInfos.Add(volunteer);
                _context.SaveChanges();
                return RedirectToAction("Index", "Home");
    
            }

    In my view, I want a dropdown for Occasions. I tried using

    @Html.DropDownFor
    @Html.DropDown

     but it just didn't work for me. Can someone kindly tell me how to get the values in the dropdown and then when the user selects an option, how to take the value of the selected option and store it into the DB? Thank you so much.


    • Diedit oleh Anan05 Kamis, 12 Juli 2018 09.43
    • Dipindahkan oleh CoolDadTx Kamis, 12 Juli 2018 14.35 ASP.NET related
    Kamis, 12 Juli 2018 09.42

Semua Balasan