英文:
Why is my ASP.NET Core MVC page trying to override the 'action' attribute for <form>?
问题
The error message you're encountering is related to an issue with the HTML form in your ASP.NET Core MVC application. Specifically, it's complaining about the use of the asp-action
attribute along with the action
attribute in your <form>
element.
Here's the translated error message:
无法覆盖
<form>
的 'action' 属性。已指定 'action' 的<form>
不得具有以 'asp-route-' 或 'asp-action'、'asp-controller'、'asp-fragment'、'asp-area'、'asp-route'、'asp-page' 或 'asp-page-handler' 属性开头的属性。
In this context, it's telling you that you should not use both the action
attribute and the asp-action
attribute within the same <form>
element. You should choose one method to specify the action for the form, either using the traditional action
attribute or the asp-action
attribute, but not both.
To resolve this issue, you can remove either the action
attribute or the asp-action
attribute based on your application's requirements.
英文:
I am trying to build an Edit form for viewing details of an entry from the home controller.
The error I get is:
> An unhandled exception occurred while processing the request.
>
> InvalidOperationException: Cannot override the 'action' attribute for <form>. A <form> with a specified 'action' must not have attributes starting with 'asp-route-' or an 'asp-action', 'asp-controller', 'asp-fragment', 'asp-area', 'asp-route', 'asp-page' or 'asp-page-handler' attribute.
>
> Microsoft.AspNetCore.Mvc.TagHelpers.FormTagHelper.Process(TagHelperContext context, TagHelperOutput output)
The stack shows
> AspNetCoreGeneratedDocument.Views_Home_Edit.ExecuteAsync() in Edit.cshtml
> Line 14: <h4 class="modal-title" id="editMemberLabel">Member @Model.MemberNumber</h4>
(see the link because the text gets chopped by the SO software)
But I don't think I have any of that in my code.
In the home controller:
[HttpGet]
public async Task<IActionResult> Edit(int id)
{
await db.Roster.SelectAsync();
var model = db.Roster.Find(x => x.ID == id);
return View("Edit", model);
}
In the Edit view, I have <div> tags with the class="form-group", but none of the other keywords that are being shown:
@model Mvc1.Models.RosterModel
@{
ViewData["Title"] = "Edit Member";
}
<h1>Edit</h1>
<hr />
<div class="modal fade" id="editMember">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="editMemberLabel">Member @Model.MemberNumber</h4>
<button type="button" class="btn-close" data-dismiss="modal">
<span>x</span>
</button>
</div>
<div class="modal-body">
<form asp-action="Edit" action="Edit">
<div class="modal-body">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="ID" class="control-label"></label>
<input asp-for="ID" class="form-control" />
<span asp-validation-for="ID" class="text-danger"></span>
</div>
I am new to ASP.NET Core MVC, so I don't understand what is this error telling me that I did wrong.
答案1
得分: 1
该错误消息告诉您,当您指定asp-action
属性时,不能同时包括action
属性。它们本质上是做相同的事情,只不过asp-
版本是由ASP.NET框架使用的。因此,只需删除另一个:
<form asp-action="Edit">
英文:
That error message is telling you that when you have the asp-action
attribute specified, you cannot also include the action
attribute. They both essentially do the same thing, with the asp-
version being the one used by the ASP.NET framework. So, just remove the other one:
<form asp-action="Edit">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论