代码之家  ›  专栏  ›  技术社区  ›  user9335731

错误:POSThttp://localhost:52880/<控制器>/更新500(内部服务器错误)

  •  0
  • user9335731  · 技术社区  · 8 年前

    我是这里的初学者。我正在做这个 ASP.NET Core MVC 我尝试加载的项目 Update.cshtml 在里面 <div id="divInventoryPageLoad"></div> 在里面 Index.cshtml 作为 PartialView 。然而,我在 <div id=“divInventoryPageLoad”></div> 在里面 指数cshtml 当我运行代码时。检查时 Inspect Element 从浏览器中,我看到了错误: POST http://localhost:52880/Inventory/Update 500 (Internal Server Error)

    指数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)
        {
            ...
        }
    

    的ActionMethod Update 当我在 breakpoints 。怎么办?

    4 回复  |  直到 8 年前
        1
  •  0
  •   Ehsan Sajjad    8 年前

    您使用的语法是在执行 发布 请求不是a 获取

    因此,当请求发出时,可能无法找到名称更新为可以接受POST请求的操作,或者可能是 POST 请求方法接受不同。

    你的行动方法是 HttpGet 你需要发送 GET 通过呼叫 load

    所以,你需要这样写:

    $('#divInventoryPageLoad').load('/Inventory/Update?id='+@ViewBag.InventoryId)
    
        2
  •  0
  •   Paul Abbott    8 年前

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

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

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

    最好的方法是将 id号 在URL中,如Ehsan所说;这将改变获得的请求,一切都会很高兴。如果您坚持将其保持在POST状态(如果它是幂等的并且只返回HTML块,那么这就没有多大意义),您需要添加 [FromBody] 到参数。

        3
  •  0
  •   Rodrigo Werlang    8 年前

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

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

    $('#divInventoryPageLoad').load('/Inventory/Update/@ViewBag.InventoryId')
    
        4
  •  0
  •   Steve0    8 年前

    从错误中显示 .load() 函数正在执行 POST 控制器动作是 GET

    在动作中加入 [HttpPost]

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

    建议

    我建议任何修改数据的操作都是 发布 仅操作,根据操作的名称,我假设它正在修改一些数据。这另一个 question ,很好地说明了原因。