c#和vue之间的POST



---编辑问题:---

我正在构建我的第一个网站,以c#web应用程序作为后端,Vue作为前端。我的GET和DELETE工作正常,但当我想运行POST时,我会遇到问题,我无法完全理解如何修复。。

据我的老师说,我的Axios连接看起来很好,问题只出在我的后端。。

我想我想做的是以某种方式将模型绑定起来,让它们相互交流?

我的数据模型是Sale.cs,我的视图模型(来自Vue(称为SaleVM.cs

Sale.cs:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Models
{
public class Sale
{
public enum Status
{
Started,
Ongoing,
Done,
Removed
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; } //++
[Required]
public string CustomerNumber { get; set; }
public int UserId { get; set; } //fk
[Required]
public string YourReference { get; set; }
public DateTime DateSold { get; set; }
public DateTime DateCreated { get; set; }
public DateTime? DateDone { get; set; }
public DateTime? DateEdited { get; set; }
public Status StatusId { get; set; }
public DateTime? DateDelivered { get; set; }
public virtual ICollection<SaleArticle> SaleArticles { get; set; }
public virtual User User { get; set; }
}
}

SaleVM:

using System.Collections.Generic;
namespace Models
{
public class SaleVM
{
public enum Status
{
Started,
Ongoing,
Done,
Removed
}
public int Id { get; set; }
public string Reference { get; set; }
public DateTime DateSold { get; set; }
public DateTime DateCreated { get; set; }
public Status StatusId { get; set; }
public int UserId { get; set; }
public virtual ICollection<ArticleRow> ArticleRows { get; set; }
public virtual SelectedCustomer SelectedCustomer { get; set; }
}
}

我想我把它们写错了,因为我只猜到了怎么写。

最后是我的销售控制器:

{
[Route("api/[controller]")]
[ApiController]
public class SaleController : ControllerBase
{
private readonly DataContext _context;
public SaleController(DataContext context)
{
_context = context;
}
// POST: api/Sales/5
[HttpPost]
public async Task<ActionResult<Sale>> PostSale(SaleVM saleVM)
{
string customerNumber = (saleVM != null && saleVM.SelectedCustomer != null && Convert.ToInt32(saleVM.SelectedCustomer.CustomerNumber) > 0) ? saleVM.SelectedCustomer.CustomerNumber : "";
List<SaleArticle> saleArticles = new List<SaleArticle>();
if (saleVM != null && saleVM.ArticleRows != null && Convert.ToInt32(saleVM.ArticleRows) > 0)
{
foreach (var item in saleVM.ArticleRows)
{
saleArticles.Add(new SaleArticle()
{
ArticleNumber = item.ArticleNumber,
Price = item.SalesPrice,
Quantity = item.Quantity,
Description = item.Description,
Id = item.Name
});
}
}
//foreach (var saleVM in SaleVM)
//{
Sale sale = new Sale();
{
sale.CustomerNumber = saleVM.SelectedCustomer.CustomerNumber;
sale.YourReference = saleVM.Reference;
sale.SaleArticles = saleArticles;
sale.DateCreated = saleVM.DateCreated;
sale.DateSold = saleVM.DateSold;
sale.StatusId = (Sale.Status)saleVM.StatusId;
sale.UserId = saleVM.UserId;
};
_context.Sales.Add(sale);
//}
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetSale), new { id = saleVM.Id }, saleVM);
}
}
}

我在visual studio中没有收到任何错误消息,但当我在poster中尝试代码时,我得到了状态代码500内部服务器错误:

System.NullReferenceException:对象引用未设置为对象的实例。位于E37SalesApi.Controllers.SaleController.PostSale(SaleVM SaleVM(,位于C:\Users\fadaka\source\repos\E37SalesApi\Controllers\SaleController.cs:line 47在lambda_method(闭包,对象(位于Microsoft.Extensions.Interal.ObjectMethodExecutiorAwaable.Awaiter.GetResult((位于Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper映射程序、ObjectMethodExecutor执行程序、Object控制器、Object[]参数(位于Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g_Await|12_0(ControllerActionInvoker调用程序,ValueTask`1 actionResultValueTask(位于Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g_Await|10_0(ControllerActionInvoker调用程序,任务lastTask,状态next,作用域范围,对象状态,布尔值isCompleted(位于Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed上下文(在Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State&Next,Scope&Scope,Object&State,Boolean&isCompleted(位于Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync((---从引发异常的前一位置开始的堆栈结尾跟踪---位于Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g_Await|19_0(ResourceInvoker调用程序,Task lastTask,State next,Scope Scope,Object State,Boolean isCompleted(位于Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g_Await|17_0(资源调用程序调用程序,任务任务,IDisposable作用域(位于Microsoft.AspNetCore.Routing.EndpointMiddleware.g_AwaitRequestTask|6_0(端点,任务请求任务,ILogger记录器(位于Microsoft.AspNetCore.Authorization.AuthenticationMiddleware.IInvoke(HttpContext上下文(位于Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.IInvoke(HttpContext上下文(

我确实理解它所说的,只是我真的不明白如何修复它并使我的POST方法完全工作。

我希望我清楚我的问题

我对编程很陌生,很难在网上找到像我这样的傻瓜能理解的信息,尤其是当涉及到c#和vue的合作时。

对于此问题

CS0019运算符">"不能应用于"string"one_answers"int "类型的操作数

而不是

saleVM.SelectedCustomer.CustomerNumber > 0

更改为

Convert.ToInt32(saleVM.SelectedCustomer.CustomerNumber) > 0

最新更新