harrisgeo
harrisgeo

Reputation: 515

How do I add a button click event on a different class in .Net MVC?

I am newbie with asp.net MVC and i want something simple. I have an Index.aspx page and a UrlContent.cs class.

I am searching how to write the code of the button_click listener of the page in the class. So far havent found anything on google.

Thats all, thank you

Upvotes: 0

Views: 1223

Answers (2)

McGarnagle
McGarnagle

Reputation: 102763

MVC is a different paradigm, and doesn't really have the concept of "event listeners".

That concept was always an abstraction from how web clients/servers really communicated. To a web server, there's really only one event, and that is an HTTP request from the client. To achieve the illusion of "events", ASP.Net does some (Javascript+cookies) magic behind the scenes, and creates hidden form input tags -- containing info about which button was clicked -- within a standard HTML form, and posting the form back to the server.

MVC adheres much more closely to the native behavior of HTML/HTTP. It requires you to get accustomed to working with those technologies -- forms, GET/POST requests, and AJAX.


To handle a (submit form) button click event, you create an action in your controller that accepts parameters.

Controller

[HttpPost]
public ActionResult Index(MyModel model)
{
    // handle the submit button's "click event" here
}

View

@model MyModel

@using (@Html.BeginForm("Index", "Home")) {
    @Html.EditorForModel
    <input type='submit' value='submit' />
})

Upvotes: 3

If you're running MVC, I think you're looking for something like this

@Html.ActionLink("Link Text", "Action method Name", "Controller name",new {} , new {})

Here's some documentation on more overloads.

Upvotes: 0

Related Questions