---EDITED QUESTION:---
I am building my very first website with c# web application as backend and Vue as frontend. My GET and DELETE works fine, but when I want to run POST I get problems I cant fully understand how to fix..
According to my teacher my Axios connection looks fine and the problem is only in my backend..
What I think I am trying to do is to somehow bind the models so they speak to eachother?
My datamodel is Sale.cs and my viewmodel (from Vue) is called 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; }
}
}
I think i have made them incorrectly because I have only guessed how to write them.
and lastly my SaleController:
{
[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);
}
}
}
I dont get any errormessages in visual studio, but when I try the code out in postman i get statuscode 500 Internal Server Error:
System.NullReferenceException: Object reference not set to an instance of an object. at E37SalesApi.Controllers.SaleController.PostSale(SaleVM saleVM) in C:\Users\fadaka\source\repos\E37SalesApi\Controllers\SaleController.cs:line 47 at lambda_method(Closure , Object ) at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult() at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
I do understand what it says, its just I cant really understand how to fix it and get my POST method to fully work.
I hope I am clear with my problem
I am very new to programming and find it very hard to find information online that is on a level that a noob like me can understand, especially when it comes to c# and vue working together.