我正在尝试使用MVC模型创建动态表。这是我的模特。
public class PrescriptionEditModel
{
[Required]
public Guid Id { get; set; }
[Required]
[Display(Name = "Medicine List")]
public List<PrescriptionMedicineModel> PrescriptionList { get; set; }
}
公共类PrescriptionMedicineModel {
[Required]
public Guid Id { get; set; }
[Required]
[Display(Name = "Medicine")]
public Guid MedicineId { get; set; }
[Required]
[Display(Name = "Prescription Duration")]
public Guid PrescriptionDurationId { get; set; }
public string NumberOf { get; set; }
}
我的控制器代码是
public ActionResult Create()
{
ViewBag.PatientId = new SelectList(db.Patients.Where(h => h.HospitalId == hp.HospitalId), "Id", "FirstName");
ViewBag.MedicineId = new SelectList(db.Medicines.Where(h => h.HospitalId == hp.HospitalId), "Id", "Name");
ViewBag.PrescriptionFrequencyId = new SelectList(db.PrescriptionFrequencies.Where(h => h.HospitalId == hp.HospitalId), "Id", "Name");
PrescriptionMedicineModel prescription = new PrescriptionMedicineModel()
{
MedicineId = Guid.Empty,
PrescriptionDurationId = Guid.Empty,
PrescriptionFrequencyId = Guid.Empty,
PrescriptionWhentoTakeId = Guid.Empty
};
List<PrescriptionMedicineModel> newPrescriptionList = new List<PrescriptionMedicineModel>();
newPrescriptionList.Add(prescription);
PrescriptionEditModel newModel = new PrescriptionEditModel()
{
CaseHistory = null,
DoctorName =null,
HospitalId = hp.HospitalId,
PatientId = Guid.Empty,
PrescriptionDate = null,
PrescriptionList = newPrescriptionList
};
return View(newModel);
}
我的观点是
<table class="table table-hover">
<thead>
<tr>
<th>Medicine Name</th>
<th>Duration</th>
</tr>
</thead>
<tbody>
@for (var i = 0; i < Model.PrescriptionList.Count; i++)
{
<tr>
<td>@Html.DropDownListFor(m => Model.PrescriptionList[i].MedicineId, new SelectList(ViewBag.MedicineId, "Id", "Name"))</td>
<td>@Html.DropDownListFor(m => Model.PrescriptionList[i].PrescriptionDurationId, new SelectList(ViewBag.PrescriptionFrequencyId, "Id", "Name"))</td>
</tr>
}
</tbody>
这给出了一条错误消息:“数据绑定:'System.Web.Mvc.SelectListItem'不包含名称为'Id'的属性。]”。
我正在尝试创建带有项目列表的药品列表,以允许用户编辑药品的详细信息。用户必须具有编辑项目的能力。
DropDownListFor不会将项目绑定到下拉列表。
任何想法
答案 0 :(得分:0)
这里是一个示例,我相信您的Id和Name字段与模型不匹配,请查看我的模型如何具有以下两个属性:
查看:
@model XYZ.Models.Adviser
<div class="form-">
<label asp-for="PracticeId" class="control-label">Practice</label>
@Html.DropDownList("PracticeId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.PracticeId)
</div>
控制器:
private void PopulatePracticesDropDownList(object selectedPractice = null)
{
var practicesQuery = from d in _context.Practice
.GroupBy(a => a.Name)
.Select(grp => grp.First())
orderby d.Name
select d;
ViewBag.PracticeId = new SelectList(practicesQuery, "ID", "Name", selectedPractice);
}
模型,它具有属性ID和名称:
public class Practice
{
public int ID { get; set; }
[Required]
[Display(Name = "Practice Name")]
public string Name { get; set; }
}
public class Adviser
{
public int ID { get; set; }
[Required]
[Display(Name = "Adviser Name")]
public string Name { get; set; }
[Required]
public int PracticeId { get; set; }
[System.ComponentModel.DataAnnotations.Schema.NotMapped]
public string Practice { get; set; }
}