2

I'm trying to figure out the best way to do it and have seen a lot of suggestions, but can't get anything to work quite right and would rather do it the best way first.

Here's the ActionResult on the Controller:

public ActionResult Details(string selectedArtist)
{
    //other code
    return View(selectedArtist)
}

Here's the javascript on the client:

$('#results').on('click', '.item', function () {
        var selectedArtist = $(this).data('artistInfo');
        var selectedId = encodeURIComponent('ARE602H1187FB3B8F8')
        var url = '@Url.Action("Details", "Artist", new { id = "__id__" })';
        window.location.href = url.replace('__id__', selectedId);
    })

Ideally, I would like the pass the selectedArtist object to the controller. It's basically a javascript class that matches a Artist model I have. For now though as a test I am just trying to pass an Id as a string to the controller and can't get it to, it just always comes up as NULL.

The request that it's building looks pretty good to me ".../Artist/Details/ARE602H1187FB3B8F". However, I'm debugging and the parameter on the controller always ends up NULL.

Am I missing something? And/or is there a better way to do this? Thanks in advance.

mrshickadance
  • 1,213
  • 4
  • 20
  • 34

1 Answers1

5

Two options. Update your route table to properly allow the route with its slug "ARE602H1187FB3B8F" to be routed to the applicable action method. Or, you could pass id as a query string parameter, thereby using the default route in your route table:

... Artist/Details?selectedArtist=ARE602H1187FB3B8F
Community
  • 1
  • 1
Alex
  • 34,899
  • 5
  • 77
  • 90
  • doesn't the action method expects something else...`selectedArtist`? – Leo Feb 05 '14 at 03:46
  • Sorry, I'm fairly new to MVC. When you say update the route table. Are you talking about the RouteConfig? Where you would be changing how it handles the URLs? i.e. url: "{controller}/{action}/{id}" ..? – mrshickadance Feb 06 '14 at 02:44
  • @mrshickadance yes, that's what i meant. – Alex Feb 06 '14 at 02:55