简体   繁体   中英

Get drop down list default selected item is not passing to the controller in Asp.net MVC

I have a problem when the user (Edit the event) it display drop down list for time , and text box for date.

But when the user submit the form the, edit action is not receiving the old value for time. This code works fine if the user changes the time value, it will get the value. otherwise. it wont.

Please help me.

**the code**

Html.DropDownListFor(x=>x.StartTime, Model.Times,
Model.Appointment.StartDate.ToString("hh:mm tt"), new {@class = "dropdownlist" })

public ActionResult Edit(int id = 0){
    Appointment app = eventService.GetEventByID(id);
   EventViewModel model = new EventViewModel() 
    { Appointment =app,
     StartTime = app.StartDate.ToString("hh:mm tt"),
      EndTime = app.EndDate.ToString("hh:mm tt"),
      Times = TimesSelectListItem, };

// Get the time portion of our date/time from our drop down lists
   DateTime startTime = Convert.ToDateTime(model.StartTime);
   DateTime endTime = Convert.ToDateTime(model.EndTime);

 // Create a new date based on the date from our date picker, and time from our drop down lists:
model.Appointment.StartDate = new DateTime(model.Appointment.StartDate.Year, model.Appointment.StartDate.Month, model.Appointment.StartDate.Day, startTime.Hour, startTime.Minute, startTime.Second);
model.Appointment.EndDate = new DateTime(model.Appointment.EndDate.Year, model.Appointment.EndDate.Month, model.Appointment.EndDate.Day, endTime.Hour, endTime.Minute, endTime.Second);

 if (model == null)   {
  return HttpNotFound();   }

PopulatePriorityDropDownList(model.Appointment.FK_PriorityID);
 PopulatePrivacyDropDownList(model.Appointment.FK_PrivacyID);
PopulateTypeDropDownList(model.Appointment.FK_AppointmentTypeID);
       return View(model);
  }


   [HttpPost]
   [ValidateAntiForgeryToken]
   public ActionResult Edit(EventViewModel model, int id, string PriorityID, string PrivacyID, string TypeID)
            {
//------------
this is in the controler...
   IEnumerable<SelectListItem> TimesSelectListItem = new[] {
                new SelectListItem{ Text="6:00 AM", Value = "6:00 AM" },
                new SelectListItem{ Text="6:30 AM", Value = "6:30 AM" },....etc}

StartTime/ StartTime : string, only to display in the view.
StartDate/ EndDate: dateTime type from db.

If the value of StartTime does not match one of the properties in the SelectList then the value of the select will be null on postback because you have used an option label in DropDownListFor() (this renders an option with no value).

Check that the value of StartTime exactly matches the value of one of the options.

If you always set the initial value then remove the 3 parameter of the DropDownListFor() method

@Html.DropDownListFor(x=>x.StartTime, Model.Times, new {@class = "dropdownlist" })

otherwise, make it a user friendly message so its clear that the user should select something

Html.DropDownListFor(x=>x.StartTime, Model.Times, "Please select a time"), new {@class = "dropdownlist" })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM