asp.net-mvcdurandaldurandal-2.0

Durandal not passing parameters to MVC controller action


I'm having a problem using parameters in Durandal for an ASP.NET MVC application...

I use MVC Route Attributes, for example: [Route("admin/pages")], [Route("admin/blog")], etc..

And I integrate Durandal as follows:

main.js:

app.start().then(function() {
    viewEngine.viewExtension = '/';

    viewLocator.convertModuleIdToViewId = function (moduleId) {
        return moduleId.replace('viewmodels', '');
    };

    app.setRoot('viewmodels/admin/shell', 'entrance');
});

This works nicely, as the viewId ends up matching the MVC route.

However, I now face a serious problem with trying to use parameters in the routes. For example, I have one route as follows:

[Route("admin/localization/localizable-strings/{languageId}")]

and my Durandal route for this is:

localization/localizable-strings/:languageId

Example anchor tag:

<a href="/admin/#localization/localizable-strings/1" class="btn btn-primary btn-xs">Localize</a>

This doesn't work, as it seems Durandal doesn't pass the parameter to the server. It simply retrieves the HTML from /admin/localization/localizable-strings which returns a 404 page because no parameter was passed to the MVC action.

What are my options here?


Solution

  • I know this question is almost 2 years old now, but I obviously forgot to close it with my solution at the time. Basically, Brett and Marvin were correct, but since I was so new to Durandal (and SPAs in general) at the time, I misunderstood exactly what they were telling me. I ended up doing this:

    self.cultureCode = null;
    
    self.activate = function (cultureCode) {
        self.cultureCode = cultureCode;
    
        if (!self.cultureCode) {
            self.cultureCode = null;
        }
    };
    

    And the durandal route was set up as follows;

    "localization/localizable-strings/:cultureCode"
    

    I would then use that parameter to obtain the relevant data via ajax. My mistake was wanting the parameter to be able to be used in the MVC action method so that I could obtain some data server side, which of course doesn't work...