Simplest way to call a controller method from a view button in .Net (mvc)

F. Iván

I just started programming at MVC and I would like to ask about the best way to call a controller's method from your view. To make it easier: I have a model called Song with 2 methods to start and stop it:

public class Song
{
        WMPLib.WindowsMediaPlayer wplayer ;

        public Song() {
            wplayer = new WMPLib.WindowsMediaPlayer();
            wplayer.URL = "my song url";
            wplayer.controls.stop();
        }
        public void Stop() {
            wplayer.controls.stop();
        }
        public void Start() {
            wplayer.controls.play();
        }
    }

In the controller I create the song object and I have two functions to start and stop it.

public class DefaultController : Controller
        {
            // GET: Default
            Song c = new Song();

            public ActionResult Index()
            {
                return View(c);
            }
            public void Start() {
                c.Start();            
            }
            public void Stop() {
                c.Stop();
            }
        }

In the view I have 2 buttons that correspond to each controller action.

<button type="button">Start</button>
<button type="button">Stop</button>

How can I do the most correct way to call each controller action from the view buttons?

Thank you very much for your attention, I appreciate your help

Saurabh Solanki

You can use set href path to call your controller from view.

<a class="btn btn-success" href="@Url.Action("Start", "DefaultController")">Start</a>

and if want to pass parameter then:

<a class="btn btn-success" href="@Url.Action("Start","DefaultController", new {id=Item.id })">Start</a>

or for other options you can check this link

you can call your controller method using ajax also refer this link

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In ASP.NET MVC: All possible ways to call Controller Action Method from a Razor View

ASP.NET MVC - How to call void controller method without leaving the view?

How to call controller from the button click in asp.net MVC 4

Call app delegate method from view controller

How to pass an id from a razor view to a controller's action method in ASP .Net MVC?

Call Controller Method Which Return View With Ajax Call From Asp.net View Page

Call a controller method in a view

ASP.NET MVC - How to call controller method and don't leave the view

Call controller method multiple times based on input in multiselect from view on submitting form [ASP.NET Core]

Can an HTML button call an MVC Controller Action method

ASP.NET MVC - How do I Call a Controller Method from within a View to Redirect to Multiple Other Views?

How to get a HTML submit button in an ASP.NET page to call a controller method in MVC controller class

How to call method of a controller from View without using ajax in ASP.NET MVC?

Check from which view method in controller got invoked ASP.NET MVC

How do I call a method on one controller from another in .NET 4 MVC

ASP.Net MVC checkbox values from view to custom controller method

Call method from controller in view (ROR)

Simplest way to get model data/objects in the view from input with an ajax partial view call(s)

Call controller function from view! asp.net MVC

Call a controller method with parameters from partial view mvc 5

Call a method from button click in ASP.net, using controller

Call controller from view and pass value from view to controller in asp.net mvc

How to call a custom identity Register method from a controller in .net core 2.1 mvc

Call correct method from Controller (MVC, RAZOR)

Mapping a HTML button to call a method in a controller class in Spring MVC

ASP.Net Mvc How to pass data from View into Controller with a Button using Ajax

Pass a variable from a view to a controller action method and then to another method in the same ASP.NET MVC controller

JSON Call from view to controller and show JSON object data in view in ASP.NET Core 6 MVC

Can I pass a value from a button in a view to the controller using C# ASP.NET MVC and Razor