3

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?

12
  • try add FromForm .... CreatePrescription([FromForm] ViewModel model){ Commented Feb 15, 2020 at 1:26
  • 1
    also your model missing model.Patient.Id Commented Feb 15, 2020 at 1:27
  • Make sure you not enabled camel casing. If that's the case, then post data using the camel cases. Commented Feb 15, 2020 at 1:45
  • @daremachine I tried adding a [FromForm] and did not help, also model.Patient.Id is fine, imagine adding a prescription to a patient, that is what I am doing, and model.Patient.Id is the only value that is getting passed Commented Feb 15, 2020 at 1:51
  • can you post your applicationContext Commented Feb 15, 2020 at 5:02

2 Answers 2

2

Here are the three observations I have made by reviewing portions of your code

  • In yourPost 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)
     }
    
  • Namespace of your 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.
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.