【问题标题】:Why is my HTTP edit post method not working?为什么我的 HTTP 编辑发布方法不起作用?
【发布时间】:2020-06-11 10:52:08
【问题描述】:

当我尝试提交表单时,我的请求未到达 Post-method 'Edit 并返回 HTTP 错误代码 415。

我的 Razor 页面没有触发操作,我正在使用 metronic 主题集成,然后无法使用 post 方法。以下是服务端代码:

[HttpPost]
[ValidateAntiForgeryToken]        
public async Task<IActionResult> Edit(int id, [FromForm] Firma firma)
{
    //code here[enter image description here][1]
}

以下是客户端代码:

@model satinalma.Models.Firma
@{
    ViewData["Title"] = "Edit";
}
<form asp-action="Edit">
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    <input type="hidden" asp-for="Id" />
    <div class="form-group">
        <label asp-for="Baslik" class="control-label"></label>
        <input asp-for="Baslik" class="form-control" />
        <span asp-validation-for="Baslik" class="text-danger"></span>
    </div>
    <div class="form-group">
        <input type="submit" value="Save" class="btn btn-primary" />
    </div>
</form>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

【问题讨论】:

  • 尝试添加asp-controller="YourControllerName" 标签,并在控制器内部删除int id。将其作为一个属性添加到您的 Firma 类中。

标签: c# asp.net-core-mvc razor-pages


【解决方案1】:

您已经获得了方法的 asp-action 标记帮助程序,但没有用于控制器的帮助程序。尝试添加它,看看它是否正确路由。

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-3.1

【讨论】:

  • 别工作!我看的更早,我不明白发生了什么,只有主题文件导入_layout.cshtml
猜你喜欢
  • 2019-06-04
  • 2020-11-29
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
  • 2021-01-30
  • 2015-03-14
  • 2014-11-03
  • 1970-01-01
相关资源
最近更新 更多