ERROR: POST http://localhost:52880/<Controller>/Update 500 (Internal Server Error)
ERROR: POST http://localhost:52880/<Controller>/Update 500 (Internal Server Error)
我是这里的初学者。我正在处理这个 ASP.NET Core MVC
项目,我试图在 Index.cshtml
中的 <div id="divInventoryPageLoad"></div>
中加载 Update.cshtml
作为 PartialView
。但是,当我 运行 代码时,我在 Index.cshtml
中的 <div id="divInventoryPageLoad"></div>
中没有得到任何输出。从浏览器检查 Inspect Element
时,我发现我收到错误:POST http://localhost:52880/Inventory/Update 500 (Internal Server Error)
Index.cshtml
<div class="pm-body clearfix">
<div role="tabpanel">
<ul class="tab-nav" role="tablist">
<li class="active" role="presentation">
<a href="#inventoryDetails" aria-controls="inventoryDetails" role="tab" data-toggle="tab" aria-expanded="true" onclick="LoadInventoryUpdateDetails(@ViewBag.InventoryId)">Inventory Details</a>
</li>
<li id="inventorylocatetab">
<a href="#inventoryLocate" aria-controls="inventoryLocate" role="tab" data-toggle="tab" aria-expanded="true" onclick="LoadInventoryLocate()">Inventory Locate</a>
</li>
</ul>
</div>
<div id="divInventoryPageLoad"></div>
</div>
@section Scripts{
<script type="text/javascript">
$(document).ready(function () {
$('#divInventoryPageLoad').load('/Inventory/Update', { Id:@ViewBag.InventoryId }, function() {
});
})
function LoadInventoryUpdateDetails(Id) {
$('#divPageLoad').load('/Inventory/Update', { Id:Id }, function() {
});
}
function Locate() {
$('#divPageLoad').load('/Inventory/Locate');
}
</script>
}
控制器
// GET: /Inventory/
public IActionResult Index(int Id)
{
ViewBag.InventoryId = Id;
return View();
}
// GET : ~/Inventory/Update/Id
public IActionResult Update(int Id)
{
...
}
当我在 breakpoints
的帮助下进行测试时,Update
的 ActionMethod 没有被命中。怎么办?
来自 http://api.jquery.com/load/
的文档
"The POST method is used if data is provided as an object; otherwise, GET is assumed."
您导致请求通过 POST 发送,但是您在控制器中设置的方式不起作用,因为它正在寻找 id
在 URL,不在请求体里面的POST数据中。
你最好的选择就是像 Ehsan 所说的那样将 id
连接到 URL 中;这会将请求更改为 GET,一切都会很开心。如果您坚持保留它 POST(如果它是幂等的并且只返回 HTML 的块,那将没有多大意义),您需要将 [FromBody]
添加到参数中。
您使用的语法是当我们执行 POST 请求而不是 GET.
因此,当请求发出时,它可能无法找到可以接受 POST 请求的名称更新的操作,或者 POST
请求方法接受的参数可能不同。
您的操作方法是 HttpGet
,您需要通过 load
发送 GET
调用。
所以,你需要这样写:
$('#divInventoryPageLoad').load('/Inventory/Update?id='+@ViewBag.InventoryId)
错误显示 .load()
函数正在执行 POST
并且控制器操作是 GET
.
用 [HttpPost]
撒上动作
[HttpPost]
public IActionResult Update(int Id)
{
...
}
建议
我建议任何修改数据的操作都是 POST
唯一的操作,从你的操作名称来看,我会假设它正在修改一些数据。另一个 question,对原因进行了很好的分析。
我觉得你用错了url。
你可以试试
$('#divInventoryPageLoad').load('/Inventory/Update?id=@ViewBag.InventoryId')
或
$('#divInventoryPageLoad').load('/Inventory/Update/@ViewBag.InventoryId')
我是这里的初学者。我正在处理这个 ASP.NET Core MVC
项目,我试图在 Index.cshtml
中的 <div id="divInventoryPageLoad"></div>
中加载 Update.cshtml
作为 PartialView
。但是,当我 运行 代码时,我在 Index.cshtml
中的 <div id="divInventoryPageLoad"></div>
中没有得到任何输出。从浏览器检查 Inspect Element
时,我发现我收到错误:POST http://localhost:52880/Inventory/Update 500 (Internal Server Error)
Index.cshtml
<div class="pm-body clearfix">
<div role="tabpanel">
<ul class="tab-nav" role="tablist">
<li class="active" role="presentation">
<a href="#inventoryDetails" aria-controls="inventoryDetails" role="tab" data-toggle="tab" aria-expanded="true" onclick="LoadInventoryUpdateDetails(@ViewBag.InventoryId)">Inventory Details</a>
</li>
<li id="inventorylocatetab">
<a href="#inventoryLocate" aria-controls="inventoryLocate" role="tab" data-toggle="tab" aria-expanded="true" onclick="LoadInventoryLocate()">Inventory Locate</a>
</li>
</ul>
</div>
<div id="divInventoryPageLoad"></div>
</div>
@section Scripts{
<script type="text/javascript">
$(document).ready(function () {
$('#divInventoryPageLoad').load('/Inventory/Update', { Id:@ViewBag.InventoryId }, function() {
});
})
function LoadInventoryUpdateDetails(Id) {
$('#divPageLoad').load('/Inventory/Update', { Id:Id }, function() {
});
}
function Locate() {
$('#divPageLoad').load('/Inventory/Locate');
}
</script>
}
控制器
// GET: /Inventory/
public IActionResult Index(int Id)
{
ViewBag.InventoryId = Id;
return View();
}
// GET : ~/Inventory/Update/Id
public IActionResult Update(int Id)
{
...
}
当我在 breakpoints
的帮助下进行测试时,Update
的 ActionMethod 没有被命中。怎么办?
来自 http://api.jquery.com/load/
的文档"The POST method is used if data is provided as an object; otherwise, GET is assumed."
您导致请求通过 POST 发送,但是您在控制器中设置的方式不起作用,因为它正在寻找 id
在 URL,不在请求体里面的POST数据中。
你最好的选择就是像 Ehsan 所说的那样将 id
连接到 URL 中;这会将请求更改为 GET,一切都会很开心。如果您坚持保留它 POST(如果它是幂等的并且只返回 HTML 的块,那将没有多大意义),您需要将 [FromBody]
添加到参数中。
您使用的语法是当我们执行 POST 请求而不是 GET.
因此,当请求发出时,它可能无法找到可以接受 POST 请求的名称更新的操作,或者 POST
请求方法接受的参数可能不同。
您的操作方法是 HttpGet
,您需要通过 load
发送 GET
调用。
所以,你需要这样写:
$('#divInventoryPageLoad').load('/Inventory/Update?id='+@ViewBag.InventoryId)
错误显示 .load()
函数正在执行 POST
并且控制器操作是 GET
.
用 [HttpPost]
[HttpPost]
public IActionResult Update(int Id)
{
...
}
建议
我建议任何修改数据的操作都是 POST
唯一的操作,从你的操作名称来看,我会假设它正在修改一些数据。另一个 question,对原因进行了很好的分析。
我觉得你用错了url。 你可以试试
$('#divInventoryPageLoad').load('/Inventory/Update?id=@ViewBag.InventoryId')
或
$('#divInventoryPageLoad').load('/Inventory/Update/@ViewBag.InventoryId')