我的ASP.NET Core MVC页面为什么要覆盖<form>标签的’action’属性?

huangapple go评论71阅读模式
英文:

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 &lt;form>. A &lt;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: &lt;h4 class="modal-title" id="editMemberLabel">Member @Model.MemberNumber&lt;/h4>

screenshot

(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&lt;IActionResult&gt; Edit(int id)
{
    await db.Roster.SelectAsync();
    var model = db.Roster.Find(x =&gt; x.ID == id);
    return View(&quot;Edit&quot;, 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[&quot;Title&quot;] = &quot;Edit Member&quot;;
}

&lt;h1&gt;Edit&lt;/h1&gt;

&lt;hr /&gt;
&lt;div class=&quot;modal fade&quot; id=&quot;editMember&quot;&gt;
    &lt;div class=&quot;modal-dialog&quot;&gt;
        &lt;div class=&quot;modal-content&quot;&gt;
            &lt;div class=&quot;modal-header&quot;&gt;
                &lt;h4 class=&quot;modal-title&quot; id=&quot;editMemberLabel&quot;&gt;Member @Model.MemberNumber&lt;/h4&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn-close&quot; data-dismiss=&quot;modal&quot;&gt;
                    &lt;span&gt;x&lt;/span&gt;
                &lt;/button&gt;
            &lt;/div&gt;
            &lt;div class=&quot;modal-body&quot;&gt;
                &lt;form asp-action=&quot;Edit&quot; action=&quot;Edit&quot;&gt;
                    &lt;div class=&quot;modal-body&quot;&gt;
                        &lt;div asp-validation-summary=&quot;ModelOnly&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
                        &lt;div class=&quot;form-group&quot;&gt;
                            &lt;label asp-for=&quot;ID&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                            &lt;input asp-for=&quot;ID&quot; class=&quot;form-control&quot; /&gt;
                            &lt;span asp-validation-for=&quot;ID&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                        &lt;/div&gt;

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:

&lt;form asp-action=&quot;Edit&quot;&gt;

huangapple
  • 本文由 发表于 2023年6月22日 00:20:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定