[英]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 PAtientId和Null Properties的調試,為什么我的值沒有被傳遞到控制器並且沒有保存到數據庫中?
以下是我通過查看部分代碼得出的三個觀察結果
CreatePrescription
的Post
方法中,檢查ModelState.IsValid
好習慣 [HttpPost] public IActionResult CreatePrescription(ViewModel model){ if(ModelState.IsValid) { try { Prescription prescription = new Prescription{ .... } } }
在CreatePrescription
的Get
方法中,確保將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.