【问题标题】:: 'The ViewData item that has the key 'PatientAppointmentID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.':'具有键 'PatientAppointmentID' 的 ViewData 项的类型为 'System.Int32' 但必须为 'IEnumerable<SelectListItem>' 类型。
【发布时间】:2019-09-23 11:12:35
【问题描述】:

我有一个下拉列表

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

它的控制器方法是:

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);
}

它正在正确填充下拉列表中的值,但是当我填写表格并在下拉列表中选择值时,它会给出以下错误:

System.InvalidOperationException: '具有键 'PatientAppointmentID' 的 ViewData 项的类型为 'System.Int32' 但必须为 'IEnumerable' 类型。'

进一步提交表单时,值将存储在如下所示的会话中,我还希望 url 中的下拉列表和患者 ID 保留用于我想在会话中添加的更多值

[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);
}

【问题讨论】:

    标签: c# asp.net-mvc session drop-down-menu asp.net-mvc-viewmodel


    【解决方案1】:

    如果其他人遇到相同情况,此问题的解决方案如下:在ViewModel中为下拉列表创建属性以从数据库中检索值并将选定的值发送到db。 p>

    CreateBillViewModel:

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

    控制器中:

      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);
      }
    

    像这样在视图中检索它:

    查看:

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

    提交表单后,如果您希望打开相同的页面并希望在下拉列表中使用相同的值,然后在 HTTPPOST 中像这样将其发回。

    [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);
    }
    

    这就是我的问题的解决方法。有关更多详细信息,可以参考此链接: ThisLink

    【讨论】:

      猜你喜欢
      • 2014-04-17
      • 2014-10-02
      • 2012-01-25
      • 1970-01-01
      • 2013-10-22
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多