Tarasov Tarasov - 6 days ago 7
C# Question

How I trigger a Method if I click on a button in asp.net mvc 5?

I'm new to asp.net mvc 5 and I want to trigger a button do something. Here is my cshtml:

...
<tr>
<td><a href="#" class="btn btn-block btn-xs btn-primary"><span class="glyphicon glyphicon-align-right"></span> Räume Anzeigen</a></td>
<td><a href="#" class="btn btn-block btn-xs btn-primary"><span class="glyphicon glyphicon-align-right"></span> Fahrzeuge Anzeigen</a></td>
<td><a href="#" class="btn btn-block btn-xs btn-primary"><span class="glyphicon glyphicon-align-right"></span> Computer Anzeigen</a></td>
<td><a href="#" class="btn btn-block btn-xs btn-primary"><span class="glyphicon glyphicon-align-right"></span> Kontakte Anzeigen</a></td>
</tr>
...


I used bootstrap. In my Controller I have this method: For Example the first button must do this:

public ActionResult Rooms()
{
DataTable table = getDummyRooms();

ViewBag.table = table;

return View("Index");
}

Answer

Use Url Action e.g. this would invoke MyController and Rooms action method :

<a href="@Url.Action("Rooms", "MyController")">Rooms</a>
Comments