After reading a few other threads it seemed like we needed to map a new route in Global.asax.cs in order to be pass multiple variables. (note: it works fine if just passing one variable)

However if passing multiple variables the first one goes through but the second one (and any other ones after that) do not pass through -- their value is null.

As an example the url value in _Layout.cshtml is var url = '/Home/DidItWork?testUserName=James%20Bond&testEmail=james.bond%40gmail.com'; -- should that be coming out differently since the MapRoute is expecting something in the form of Home/DidItWork/{testUserName}/{testEmail}?

Thoughts or suggestions for why it's not passing multiple variables?

Global.asax.cs

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "MyNewRoute",                                              // Route name 
        "Home/DidItWork/{testUserName}/{testEmail}",                           // URL with parameters 
        new { controller = "Home", action = "DidItWork" }  // Parameter defaults
    );

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
}

HomeController.cs

public async Task<ActionResult> DidItWork(string testUserName, string testEmail)
{
        var manager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        var user = new ApplicationUser { UserName = "user name", Email = "username@gmail.com" };
        var result = await manager.CreateAsync(user, "passwordGoesHere1!");
        if (result.Succeeded)
        {
            await manager.SendEmailAsync(user.Id, testUserName, testEmail);

            return View();
        }
        return View();    
}

_Layout.cshtml

    @*script for someone who presses the send/email button*@ 
<script>
$("#sendButton").click(function () {
    var htmlBody = $("#myModal .modal-body").text();
    var url = '@Url.Action("DidItWork", "Home", new { testUserName = "James Bond", testEmail = "james.bond@gmail.com" })';
    $.ajax({
        url: url,
        success: function (result) {
            alert("Success. It worked." + htmlBody);
        },
        error: function (xhr, status, errorThrown) {
            alert("Sorry, there was a problem! Error: " + errorThrown + ". Status: " + status + ". Console: " + xhr);
            console.log("Hello");
            console.log("Error: " + errorThrown);
            console.log("Status: " + status);
            console.dir(xhr);
            console.log("Good-bye");
        },
    });
    return false;
});
</script>

Related posts

Recent Viewed