繁体   English   中英

在 ASP.NET Core 中发布到控制器时,所有值都为空

[英]All values are null when posting to controller in ASP.NET Core

当我将值插入视图模型并调试时,控制器中的所有值都为空,我有一个[HttpGet] public IActionResult CreatePrescription(int id)来获取视图模型,然后我有

[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();
    }

这是视图的一部分

@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>

这是课程

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;}
    }
}

这是我的数据库上下文

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);
    }
}

IEnumerable<Prescription> Prescriptions {get;set;}中的所有值都在名为 Prescription 的类中,这是显示接受 PatientId 但所有其他属性为 null PAtientIdNull Properties的调试,为什么我的值没有被传递到控制器并且没有保存到数据库中?

以下是我通过查看部分代码得出的三个观察结果

  • CreatePrescriptionPost方法中,检查ModelState.IsValid好习惯
     [HttpPost] public IActionResult CreatePrescription(ViewModel model){ if(ModelState.IsValid) { try { Prescription prescription = new Prescription{ .... } } }
  • CreatePrescriptionGet方法中,确保将model返回到View

     [HttpGet] public IActionResult CreatePrescription(int id) { //Any other business logic var model = new ViewModel(); return View(model) }
  • 您的ViewModel类的命名空间被定义为Systemz.Models,ViewModels和一个逗号,在您的视图中,您将其称为Systemz.Models.ViewModels.ViewModel

原来它不工作的原因是因为View只允许其中一种form 我不知道它只会读取我认为的第一个form ,所以问题解决了。 我有<form asp-action="CreatePrescriber" asp-controller="Patients" method="Post"><form asp-action="CreatePrescription" asp-controller="Patients" method="Post"> 它需要一个<form />并在此之后停止阅读。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM