user22019322
user22019322

Reputation:

Why is my ASP.NET Core MVC page trying to override the 'action' attribute for <form>?

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>

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<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 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.

Upvotes: 1

Views: 685

Answers (1)

DavidG
DavidG

Reputation: 118947

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">

Upvotes: 0

Related Questions