I am coding a basic shopping website using asp.net and c#. I use mvc style for this project. For every table (customer, producer, item...) I have different model, view and controllers. In every controller I have a NewX(...) function that creates a row with the infos which comes from view, for related table.
I am tryig to send customer_id value to NewItem function but RedirectToAction() method does not pass the value. Here is the end of my customer function which placed in AddCustomerController (key is Guid type customer_id):
return RedirectToAction("NewItem", "AddItem", new { customer_id = key });
I create an item in NewItem view as
@model ASPNETAOP.Models.AddItem
@{
ViewData["Title"] = "NewItem";
}
<hr />
<h1>Add Item</h1>
<div class="row">
<div class="col-md-9">
<form asp-action="NewItem">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<div class="form-group">
<label asp-for="name" class="control-label"></label>
<input asp-for="name" class="form-control" type="text">
<span asp-validation-for="name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="price" class="control-label"></label>
<input asp-for="price" class="form-control" type="number">
<span asp-validation-for="price" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<input type="submit" value="Add" class="btn btn-primary" />
</div>
<b>@ViewData["Message"]</b>
</form>
</div>
and I want to send this item to function named NewItem (I give the headline below) -which is in AddItemController- with customer_id and producer_id
public IActionResult NewItem(AddItem item, Guid customer_id, Guid? producer_id){...}
In NewItem function when I add the Console.WriteLine(customer_id); line, it gives me the 00000000-0000-0000-0000-000000000000 as customer_id but the true value of customer_id is 150201CD-6CE5-48BB-A3D5-71BBA9ED5615 so I get
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Item__custome__2A1A007B". The conflict occurred in database "SHOP", table "dbo.Customer", column 'ID'. The statement has been terminated. error.
Also when I change the customer_id value to id it works correctly but as shown sometimes I call NewItem function with two parameters. I also tried
if (ModelState.IsValid)
{
return RedirectToAction("NewItem", "AddItem", new { customer_id = key });
}
but it does not works correctly too. How can I fix this problem?