When I insert values into view model and debug, all of the values are null in the controller, I have an
[HttpGet]
public IActionResult CreatePrescription(int id) that gets the view model and then I have
[HttpPost]
public IActionResult CreatePrescription(ViewModel model){
try
{
Prescription prescription = new Prescription
{
PatientId = model.Patient.Id,
MedicationId = model.Prescription.MedicationId,
RxNumber = model.Prescription.RxNumber,
Frequency = model.Prescription.Frequency,
Quantity = model.Prescription.Quantity,
Copay = model.Prescription.Copay,
RefillsRemaining = model.Prescription.RefillsRemaining,
FolderStatusId = model.Prescription.FolderStatusId,
PrescriberId = model.Prescription.PrescriberId,
OriginalRxDate = model.Prescription.OriginalRxDate,
DateFilled = model.Prescription.DateFilled,
ExpiryDate = model.Prescription.ExpiryDate,
DeliveryDate = model.Prescription.DeliveryDate,
DeliveryTime = model.Prescription.DeliveryTime,
BillDate = model.Prescription.BillDate,
ApplicationUserId = user,
CreatedOn = DateTime.Now,
IsActive = true
};
_context.Add(prescription);
_context.SaveChanges();
}
catch (Exception)
{
throw;
}
return View();
}
Here is part of the view
@model Systemz.Models.ViewModels.ViewModel
<div class="modal-body">
<form asp-action="CreatePrescription" asp-controller="Patients" method="post">
<input asp-for="Prescription.Id" type="hidden" />
<input asp-for="Patient.Id" type="hidden" />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<div class="row">
<div class="col-sm-3">
<label asp-for="Prescription.RxNumber" class="custom-label"></label>
</div>
<div class="col-sm-7">
<input asp-for="Prescription.RxNumber" class="form-control" />
</div>
<span asp-validation-for="Prescription.RxNumber" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-3">
<label asp-for="Prescription.Frequency" class="custom-label"></label>
</div>
<div class="col-sm-7">
<input asp-for="Prescription.Frequency" class="form-control" />
</div>
<span asp-validation-for="Prescription.Frequency" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-3">
<label asp-for="Prescription.Quantity" class="custom-label"></label>
</div>
<div class="col-sm-7">
<input asp-for="Prescription.Quantity" class="form-control" />
</div>
<span asp-validation-for="Prescription.Quantity" class="text-danger"></span>
</div>
</div>
And here is the class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Systemz.Models,ViewModels
{
public class ViewModel
{
public Prescription Prescription {get;set;}
public IEnumerable<Prescription> Prescriptions {get;set;}
}
}
Here is my db context
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Systemz.Models;
namespace Systemz.Data
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
{
}
public ApplicationDbContext(DbContextOptions<ApplicationDbContext>
options)
: base(options)
{
}
public DbSet<Patient> Patients { get; set; }
public DbSet<Address> Addresses { get; set; }
public DbSet<PhoneNumber> PhoneNumbers { get; set; }
public DbSet<Prescriber> Prescribers { get; set; }
public DbSet<Prescription> Prescriptions { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<PatientPrescriber>()
.HasKey(bc => new { bc.PatientId, bc. PrescriberID });
builder.Entity<PatientPrescriber>()
.HasOne(bc => bc.Patient)
.WithMany(b => b.PatientPrescribers)
.HasForeignKey(bc => bc.PatientId);
builder.Entity<PatientPrescriber>()
.HasOne(bc => bc.Prescriber)
.WithMany(c => c.PatientPrescribers)
.HasForeignKey(bc => bc.PrescriberId);
base.OnModelCreating(builder);
}
}
All of the values that are in IEnumerable<Prescription> Prescriptions {get;set;} are in a class called Prescription,
Here is the debug that shows a PatientId being accepted but all other properties are null
PAtientId, and Null Properties,
why are my values not being passed into the controller and not saving to the database?
model.Patient.Idmodel.Patient.Idis the only value that is getting passed