【问题标题】:ERROR: POST http://localhost:52880/<Controller>/Update 500 (Internal Server Error)错误:POST http://localhost:52880/<Controller>/Update 500(内部服务器错误)
【发布时间】:2018-03-21 18:03:20
【问题描述】:

我是这里的初学者。我正在处理这个ASP.NET Core MVC 项目,我正在尝试将Update.cshtml 中的&lt;div id="divInventoryPageLoad"&gt;&lt;/div&gt; 中的Index.cshtml 作为PartialView 加载。但是,当我运行代码时,Index.cshtml 中的&lt;div id="divInventoryPageLoad"&gt;&lt;/div&gt; 没有任何输出。在从浏览器检查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 没有受到影响。怎么办?

【问题讨论】:

  • 你试过像:$('#divInventoryPageLoad').load('/Inventory/Update?id='+@ViewBag.InventoryId) 吗?
  • @EhsanSajjad。有效!您能否将其发布为答案,以便我将其标记为正确答案?
  • 添加为答案。

标签: jquery asp.net-mvc asp.net-core-mvc partial-views


【解决方案1】:

您使用的语法是当我们执行 POST 请求而不是 GET 时。

因此,当请求进行时,它可能无法找到可以接受 POST 请求的名称更新操作,或者可能是 POST 请求方法接受的参数不同。

您的操作方法是HttpGet,您需要通过load 发送GET 调用。

所以,你需要这样写:

$('#divInventoryPageLoad').load('/Inventory/Update?id='+@ViewBag.InventoryId)

【讨论】:

    【解决方案2】:

    来自http://api.jquery.com/load/的文档

    "如果数据作为对象提供,则使用 POST 方法;否则,假定为 GET。"

    您导致请求通过 POST 发送,但是您在控制器中设置的方式不起作用,因为它正在寻找 id 在 URL 中,而不是在 POST 数据中请求正文。

    您最好的选择就是像 Ehsan 所说的那样在 URL 中连接 id;这会将请求更改为 GET,一切都会很愉快。如果您坚持将其保留为 POST(如果它是幂等的并且仅返回 HTML 块,这将没有多大意义),则需要将 [FromBody] 添加到参数中。

    【讨论】:

      【解决方案3】:

      我认为您使用了错误的网址。 你可以试试

      $('#divInventoryPageLoad').load('/Inventory/Update?id=@ViewBag.InventoryId')
      

      $('#divInventoryPageLoad').load('/Inventory/Update/@ViewBag.InventoryId')
      

      【讨论】:

        【解决方案4】:

        出现在.load()函数正在执行POST并且控制器动作是GET的错误。

        使用[HttpPost] 进行操作

        [HttpPost]
        public IActionResult Update(int Id)
        {
            ...
        }
        

        建议

        我建议任何修改数据的操作都是 POST 唯一的操作,从您的操作名称来看,我认为它正在修改一些数据。另一个question 很好地解释了原因。

        【讨论】:

          猜你喜欢
          • 2017-08-17
          • 1970-01-01
          • 2016-07-29
          • 2012-02-23
          • 2023-02-20
          • 2012-03-16
          • 2021-09-07
          • 2019-02-16
          • 2023-03-08
          相关资源
          最近更新 更多