问题描述
我正在使用 asp.net 和 c# 编写一个基本的购物网站。我在这个项目中使用 mvc 风格。对于每个表(客户、生产者、项目...),我有不同的模型、视图和控制器。在每个控制器中,我都有一个 NewX(...) 函数,它为相关表创建一行,其中包含来自视图的信息。
我正在尝试将 customer_id 值发送到 NewItem 函数,但 RedirectToAction()
方法没有传递该值。这是放置在 AddCustomerController 中的客户函数的结尾(键是 Guid 类型 customer_id):
return RedirectToAction("NewItem","AddItem",new { customer_id = key });
我在 NewItem 视图中创建一个项目
@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>
并且我想将此项目发送到名为 NewItem 的函数(我在下面给出标题)——它在 AddItemController 中——带有 customer_id 和 producer_id
public IActionResult NewItem(AddItem item,Guid customer_id,Guid? producer_id){...}
在 NewItem 函数中,当我添加 Console.WriteLine(customer_id);
行时,它给了我 00000000-0000-0000-0000-000000000000
作为 customer_id 但 customer_id 的真实值是 150201CD-6CE5-48BB-A3D5-71BBA9ED5615
所以我得到
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.
错误。
此外,当我将 customer_id
值更改为 id
时,它可以正常工作,但如图所示,有时我会使用两个参数调用 NewItem 函数。我也试过
if (ModelState.IsValid)
{
return RedirectToAction("NewItem",new { customer_id = key });
}
但它也不能正常工作。我该如何解决这个问题?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)