0

I want to call an action I've got in my controller class with a Html.ActionLink from my index page but I'm getting an error and can't understand why.

Can anyone help me?

The Test action in HomeController.cs is the action I want to call.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Microsoft.Exchange.WebServices.Data;

namespace ExchangeRazor.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index ()
        {
            var mvcName = typeof(Controller).Assembly.GetName ();
            var isMono = Type.GetType ("Mono.Runtime") != null;

            ViewData ["Version"] = mvcName.Version.Major + "." + mvcName.Version.Minor;
            ViewData ["Runtime"] = isMono ? "Mono" : ".NET";

            return View ();
        }


        public ActionResult Test()
        {

            ExchangeService service = new ExchangeService ();
            //CREDENTIALS!//
            service.Credentials = new WebCredentials ("*****@****.com", "******");
            service.Url = new Uri ("https://outlook.office365.com/EWS/Exchange.asmx");
            service.TraceEnabled = true;
            service.TraceFlags = TraceFlags.All;


            EmailMessage email = new EmailMessage (service);
            email.ToRecipients.Add ("********");
            email.Subject = "MacHallo";
            email.Body = new MessageBody ("Melding fra mac exchange webservices API");
            email.Send ();


            return RedirectToAction("Index");
        }
            
    }
}

Index.cshtml:

<h2>Welcome to ASP.NET MVC @ViewData["Version"] on @ViewData["Runtime"]!</h2>

<button>@Html.ActionLink("Send", "Test")</button>

Edit:

System.MissingMethodException Method not found: 'System.Web.Routing.RouteCollection.get_AppendTrailingSlash'.

Description: HTTP 500.Error processing request. Details: Non-web exception. Exception origin (name of application or object): System.Web.Mvc.

Community
  • 1
  • 1
The Dude
  • 1,088
  • 7
  • 16
  • 30
  • 3
    What error are you getting, and where? – PiousVenom Mar 17 '15 at 13:46
  • Which part of the code? Xamarin isnt a language.. – The Dude Mar 17 '15 at 14:10
  • Where do you get this error message? – ataravati Mar 17 '15 at 14:24
  • I think this is because you use Xamarin, which referenced to Mono instead of .NET. Although Mono can run MVC 5, it seems that it's lack "get_AppendTrailingSlash" on "RouteCollection". Therefore, any uri generation methods like Html.ActionLink, Html.Form, etc will throw this error. My suggestion is either stick with MVC 4, or switch to .NET entirely. – Firanto May 13 '15 at 09:54

3 Answers3

3

You could simply do the following:

<form action="~/.../Test" method="post">
     <input type="submit" id="btnSend" value="Send" />
</form>

Please note, you'll need to map accordingly to your Test controller.

That would automatically trigger your content as a form submit assuming you have more information to submit. This would then call your controller Test. Your other approach would be to use Ajax, which you could also trigger a post.

Greg
  • 11,302
  • 2
  • 48
  • 79
  • [Perfect match](http://stackoverflow.com/questions/30497900/method-routecollection-get-appendtrailingslash-not-found-when-using-razor-url/30561228#30561228) – Zameer Ansari May 31 '15 at 18:50
  • @Zameeramir you don't do much web development if you haven't seen a simple form post. – Greg Jun 01 '15 at 00:28
  • Yeah, I accept that!!! :) I'm emphasizing that we tend to go extra geeky when we have simple enough solutions. – Zameer Ansari Jun 01 '15 at 05:24
2

Since your action Test is producing side effects I recommend using a POST instead of a GET.

Change the link to a form with a button.

<h2>Welcome to ASP.NET MVC @ViewData["Version"] on @ViewData["Runtime"]!</h2>

@using(Html.BeginForm("Test", "Home", FormMethod.Post))
{
    <button type="submit">Send</button>
}

Then decorate the controller method.

[HttpPost]
public ActionResult Test() { /* ... */ }
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
1

Dont have the opportunity to try at the moment, but you could try removing the [HttpPost].

mathletics
  • 271
  • 1
  • 4
  • 15
  • A Controller should be decorated with some form of `Http` deceleration. Each controller correlates some state for the page, ie: `Get, Put, Delete, Post` – Greg Mar 17 '15 at 14:17
  • @Greg it seems as though this is a method inside a controller, could be wrong though. – mathletics Mar 17 '15 at 18:17