Multiple ways to call Controller Action Method from a Razor View

ASP.NET MVC allows you to call Controller Action Methods from Razor Views in various ways. Here are some of the possible methods:

1. HTML Forms

<form method="post" asp-action="ActionName" asp-controller="ControllerName">
    <!-- form fields go here -->
    <button type="submit">Submit</button>

This approach is suitable for scenarios where you want to submit data to the server using a form.

<a asp-controller="ControllerName" asp-action="ActionName">Click me</a>

You can use anchor tags to create hyperlinks that navigate to a specific action method.

3. Ajax Requests

    function callActionMethod() {
            url: '@Url.Action("ActionName", "ControllerName")',
            type: 'GET',
            success: function (data) {
                // handle the response

<button onclick="callActionMethod()">Call Action Method</button>

You can use JavaScript/jQuery to make asynchronous requests to Controller Action Methods.

4. Using Tag Helpers

<a asp-controller="ControllerName" asp-action="ActionName">Click me</a>

Tag Helpers simplify the syntax for creating links in Razor Views, making it more readable.

5. Route Parameters

<a asp-controller="ControllerName" asp-action="ActionName" asp-route-id="1">Click me</a>

If your action method expects parameters, you can pass them directly in the anchor tag.

6. Redirect


You can use Response.Redirect to navigate to another action method programmatically.

7. Ajax with Razor Syntax

<button id="btnCallAction">Call Action Method</button>
@section scripts {
        $("#btnCallAction").click(function () {
            $.get('@Url.Action("ActionName", "ControllerName")', function (data) {
                // handle the response

This approach combines Razor syntax with Ajax for a concise and dynamic way to call action methods.

These are some common ways to call Controller Action Methods from Razor Views in ASP.NET MVC. Choose the method that best fits your scenario and requirements.

Asp.Net Ajax Control Toolkit tutorials.

Give your valuable comments.

4 + 5 =

About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice