C# 在ASP.NET Core中过帐到控制器时,所有值均为空
当我在视图模型和调试中插入值时,控制器中的所有值都为null,我有一个C# 在ASP.NET Core中过帐到控制器时,所有值均为空,c#,asp.net,controller,C#,Asp.net,Controller,当我在视图模型和调试中插入值时,控制器中的所有值都为null,我有一个 [HttpGet] public IActionResult CreatePrescription(int-id)获取视图模型,然后 [HttpPost] public IActionResult CreatePrescription(ViewModel model){ try { Prescription prescription = new Prescription
[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>
@model Systemz.Models.ViewModels.ViewModel
这是课堂
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;}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
命名空间Systemz.Models、ViewModels
{
公共类视图模型
{
公共处方{get;set;}
公共IEnumerable处方{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);
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Identity.EntityFrameworkCore;
使用Microsoft.EntityFrameworkCore;
使用Systemz.模型;
命名空间Systemz.Data
{
公共类ApplicationDbContext:IdentityDbContext,和,
为什么我的值没有被传递到控制器中,也没有保存到数据库中?下面是我通过查看部分代码得出的三个观察结果
- 在您的
Post
方法中,检查ModelState.IsValid
[HttpPost]
public IActionResult CreatePrescription(ViewModel model){
if(ModelState.IsValid)
{
try
{ Prescription prescription = new Prescription{ .... } }
}
- 在
Get
方法的CreatePrescription
中,确保将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
结果表明,它不起作用的原因是视图中只允许一个表单
。我不知道它只会读取我视图中的第一个表单,所以问题得到了解决
和
。需要一个
,然后停止阅读。尝试添加FromForm….CreatePrescription([FromForm]ViewModel model){并且您的模型缺少model.Patient.Id
确保您没有启用驼峰壳。如果是这种情况,则使用驼峰壳发布数据。@daremachine我尝试添加了[FromForm]而且没有帮助,还有model.Patient.Id很好,想象一下为患者添加处方,这就是我正在做的,并且model.Patient.Id
是唯一被传递的值。您可以发布您的应用程序上下文吗