What are asp-controller and asp-action equal to in the statement <button type="submit" class="btn btn-primary" style="width:150px">Create</button>?

Cecilia H 1 Reputation point
2022-12-04T05:24:16.527+00:00

Could you please let me know what asp-controller and asp-action equal to in the statement <button type="submit" class="btn btn-primary" style="width:150px">Create</button>? Please refer to the attachment for details. Thank you in advance for your help.266902-capture.png

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,164 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yiyi You - MSFT 521 Reputation points
    2022-12-06T06:08:25.297+00:00

    Hi,@Cecilia H ,

    If you add asp-controller and asp-action to your button,it will generate to formaction.The asp-controller attribute assigns the controller used for generating the URL. The asp-action attribute value represents the controller action name.Here is a simple demo:

      <form>  
    <button asp-controller="Category" asp-action="Test" type="submit" class="btn btn-primary" style="width:150px">Create</button>  
     </form>  
    

    result:

    267607-capture1.png

    So that when you clicking the button,it will submit the form and go to Category/Test.

    If you want to add asp-controller and asp-action to <a> tag.it will generate to href.

    <a asp-controller="Category" asp-action="Test" type="submit" class="btn btn-secondary" style="width:150px">Back to List</a>  
    

    result:
    268561-capture2.png
    When clicking the button,it willgo to Category/Test without submitting the form.And for more details about anchor helper,you can refer to the official doc.

    ----------

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Yiyi You

    0 comments No comments