0

I have a from in which there is a dropdownlist

 @Html.DropDownListFor(m => m.PatientAppointmentID, ViewBag.AppointmentDate as List<SelectListItem>," - Select Appointment Date -",new { @class = "form-control" })

The controller method for it is:

public ActionResult Create(int? Patient_id)
{
    CreateBillViewModel model = new CreateBillViewModel();
    if (Patient_id != null)
    {
        List<SelectListItem> AppointmentDates = (from p in db.tblPatientAppointments
                                                 where p.patient_id == Patient_id
                                                 select new SelectListItem
                                                 {
                                                     Text = p.AppointmentDate,
                                                     Value = p.ID.ToString()
                                                 }).ToList();
        ViewBag.AppointmentDate = AppointmentDates;
    }
    return View(model);
}

It is populating the values in drop down correctly but when I fill the form and select value in drop down list it is giving the following error:

System.InvalidOperationException: 'The ViewData item that has the key 'PatientAppointmentID' is of type 'System.Int32' but must be of type 'IEnumerable'.'

Further on submitting the form the values are stored in a session like given below,I also want the drop down and patient id in the url to retain for further values that I want to add in the session

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(string nameValueInsert, string nameValueSubmit, CreateBillViewModel model)
{
    var button = nameValueInsert ?? nameValueSubmit;
    if (button == "Insert")
    {
        if (Session["templist"] == null)
        {

            List<PatientViewModel> lst = new List<PatientViewModel>();

            lst.Add(new PatientViewModel()
            {
                PatientAppointmentID = Int32.Parse(Request.Form["PatientAppointmentID"]),
                                        Amount = double.Parse(Request.Form["Amount"]),
                Description = Request.Form["Description"]
            });
            Session["templist"] = lst;
        }
        else
        {
            List<PatientViewModel> lst = (List<PatientViewModel>)Session["templist"];

            lst.Add(new PatientViewModel()
            {
                PatientAppointmentID = Int32.Parse(Request.Form["PatientAppointmentID"]),
                                        Amount = double.Parse(Request.Form["Amount"]),
                Description = Request.Form["Description"]
            });
            Session["templist"] = lst;
        }
    }
    else
    {
        if (ModelState.IsValid)
        {
            string username = "";
            HttpCookie cookie = HttpContext.Request.Cookies["AdminCookies"];
            if (cookie != null)
            {
                username = Convert.ToString(cookie.Values["UserName"]);
            }
            tblPatientBill patientbill = new tblPatientBill();
            patientbill.PatientAppointmentID = model.PatientAppointmentID;
            patientbill.Amount = model.AmountTotal;
            patientbill.Description = model.Note;
            patientbill.Discount = model.Discount;
            patientbill.CreatedAt = model.CreatedAt;
            patientbill.CreatedBy = username;
            patientbill.is_active = true;
            db.tblPatientBills.Add(patientbill);
            db.SaveChanges();

            int PatientBill_ID = Convert.ToInt32(patientbill.ID);
            List<PatientViewModel> lst = (List<PatientViewModel>)Session["templist"];
            if (lst != null)
            {
                tblPatientBillDetail billdetail = new tblPatientBillDetail();
                foreach (var item in lst)
                {
                    billdetail.PatientBillID = PatientBill_ID;
                    billdetail.Amount = item.Amount;
                    billdetail.CreatedAt = DateTime.UtcNow;
                    billdetail.CreatedBy = username;
                    billdetail.Description = item.Description;
                    billdetail.is_active = true;
                    db.tblPatientBillDetails.Add(billdetail);
                    db.SaveChanges();
                }
                Session.Clear();
            }
            return RedirectToAction("Print", new { Billid = @PatientBill_ID });
        }
    }
    return View(model);
}
Airn5475
  • 2,452
  • 29
  • 51
Sadia
  • 171
  • 1
  • 5
  • 25

1 Answers1

0

The solution to this problem is as given below if somebody else gets into the same situation: In ViewModel create properties for dropdown list to retrieve values from database and send the selected value to db.

CreateBillViewModel:

public class CreateBillViewModel
{
-------- --------
public IEnumerable<SelectListItem> PatientAppointmentDate { get; set; }
public int PatientAppointmentID { get; set; }
-------- --------
}

In the Controller:

  public ActionResult Create(int? Patient_id)
  {
  CreateBillViewModel model = new CreateBillViewModel();
  if (Patient_id != null)
  {
  model.PatientAppointmentDate = (from p in db.tblPatientAppointments
  where p.patient_id == Patient_id
  select new SelectListItem
  {
  Text = p.AppointmentDate,
  Value = p.ID.ToString()
  }).ToList();
  }
  return View(model);
  }

Retrieve it in the view like this:

View:

@Html.DropDownListFor(m => m.PatientAppointmentID, Model.PatientAppointmentDate ?? new List<SelectListItem>(), "- Select Appointment Date - ", new { @class = "form-control" })

Upon submitting the form if you want the same page to open and want the same values in dropdown then in HTTPPOST send it back like this.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CreateBillViewModel model, int? Patient_id)
{
if (ModelState.IsValid)
{
//Save into db
}
model.PatientAppointmentDate = (from p in db.tblPatientAppointments
where p.patient_id == Patient_id
select new SelectListItem
{
Text = p.AppointmentDate,
Value = p.ID.ToString()
}).ToList();
}
return View(model);
}

Thats how my issue was resolved.For further details this link can be referenced: ThisLink

Sadia
  • 171
  • 1
  • 5
  • 25