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?
Here are the three observations I have made by reviewing portions of your code
Post
method of CreatePrescription
its good practice to check ModelState.IsValid
[HttpPost] public IActionResult CreatePrescription(ViewModel model){ if(ModelState.IsValid) { try { Prescription prescription = new Prescription{ .... } } }
In your Get
method of CreatePrescription
make sure you're returning model
to View
.
[HttpGet] public IActionResult CreatePrescription(int id) { //Any other business logic var model = new ViewModel(); return View(model) }
ViewModel
class is defined as Systemz.Models,ViewModels
with a comma and in Your View you have referred to it as Systemz.Models.ViewModels.ViewModel
.It turns out the reason that it wasn't working is because the View
only allows one form
in it. I didn't know that it would only read the first form
on my view, so problem solved. I had <form asp-action="CreatePrescriber" asp-controller="Patients" method="Post">
and <form asp-action="CreatePrescription" asp-controller="Patients" method="Post">
. It would take one <form />
and stop reading after that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.