У меня есть класс, который используется в частичном представлении, однако частичное представление не работает должным образом. В основном у меня есть пациенты, а частичное представление - это форма для добавления рецептов пациентам (_createprescription.cs html), но она не работает и по какой-то причине (_editprescription.cs html) работает. Поэтому вместо заполнения формы для _createprescription.cs html я хочу просто автоматически заполнить _createprescription.cs html значениями по умолчанию, а затем go и использовать _editprescription.cs html для заполнения правильных Информация. Итак, мой вопрос: лучше ли использовать что-то вроде установки значений по умолчанию в классе,
public class Prescription
{
[Key]
public int Id {get;set;}
[System.ComponentModel.DefaultValue("Medication name 50 mg")]
public string Medicine {get; set;}
}
или установки значений по умолчанию в моем представлении или контроллере. Я чувствую, что было бы лучше установить значения по умолчанию в моем контроллере, но я не уверен, как. Кто-нибудь может помочь? При отладке код идет от _context.SaveChanges();
и переходит прямо к throw
Вот контроллер
public IActionResult CreatePrescription(int patientId)
{
PatientDetailsViewModel model = new PatientDetailsViewModel
PatientDetailsViewModel model = new PatientDetailsViewModel
{
Patient = _context.Patients.Include(p => p.Prescriptions).ThenInclude(p => p.Medication).SingleOrDefault(p => p.Id == patientId)
};
ViewData["FolderStatusId"] = new SelectList(_context.FolderStatuses, "Id", "Status");
ViewData["UnitId"] = new SelectList(_context.Set<Unit>(), "Id", "UnitName");
//ViewData["PrescriberId"] = new SelectList(_context.PatientPrescribers.Where(p => p.PatientId == patientId).Select(pre => pre.Prescriber),"Id", "DoctorInfo");
ViewData["PrescriberId"] = new SelectList(_context.Prescribers, "Id", "DoctorInfo");
return PartialView("_CreatePrescription", model);
}
Метод отправки:
[HttpPost]
public JsonResult CreatePrescription(PatientDetailsViewModel model)
{
var result = false;
try
{
var user = _userManager.GetUserId(User);
var prescription = new Prescription
{
PatientId = model.Patient.Id,
MedicationId = 10010,
RxNumber = 0,
Frequency = "Twice Daily",
Quantity = 10,
Copay = 1.00m,
RefillsRemaining = 0,
FolderStatusId = 1,
PrescriberId = 3405,
OriginalRxDate = DateTime.Now,
DateFilled = DateTime.Now,
ExpiryDate = DateTime.Now,
DeliveryDate = DateTime.Now,
DeliveryTime = " ",
BillDate = DateTime.Now,
ApplicationUserId = user,
CreatedOn = DateTime.Now,
IsActive = true
};
Modification modification = new Modification
{
ActionPerformed = "Prescription created.",
LastModified = DateTime.UtcNow,
PatientId = model.Patient.Id,
ApplicationUserId = user
};
_context.Add(modification);
_context.Add(prescription);
ViewData["FolderStatusId"] = new SelectList(_context.FolderStatuses, "Id", "Status", prescription.FolderStatusId);
ViewData["UnitId"] = new SelectList(_context.Set<Unit>(), "Id", "UnitName");
//ViewData["PrescriberId"] = new SelectList(_context.PatientPrescribers.Where(p => p.PatientId == prescription.PatientId).Select(pre => pre.Prescriber), "Id", "DoctorInfo", prescription.PrescriberId);
ViewData["PrescriberId"] = new SelectList(_context.Prescribers, "Id", "DoctorInfo");
_context.SaveChanges();
result = true;
return Json(result);
}
catch (Exception)
{
throw;
}