1

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?

Matt
  • 6,787
  • 11
  • 65
  • 112
  • Are you using MVC responses as your Durandal view? i.e The ViewResult from a controller action is the html view for your Durandal viewModel. – marvc1 Aug 01 '15 at 09:57
  • @MarvinRounce, yes I am. – Matt Aug 01 '15 at 23:49
  • Durandal and SPA's in general are about putting the logic and processing onto the client, rather than the server. This is one of main reasons for going with a SPA architecture in the first place. By using MVC and Durandal you have two steps of processing, which suggests there may be a problem with your overall architecture and/or reasons for using Durandal. Anyhow, if you really want to do it, you'll need to implement a custom viewEngine.js. If you manage it, it would interesting to see the code. – marvc1 Aug 02 '15 at 08:29
  • Why is it that when people don't why some other dev wants to do something unusual, they assume that there must be something wrong with the design/architecture? Is it really that unreasonable for an ASP.NET MVC developer to want to use an SPA framework and to take advantage of what both technologies have to offer? I have Durandal working very nicely in my app now and this was the last hurdle. So far, I have worked around the issue by grabbing the parameters in the `activate()` function instead. – Matt Aug 03 '15 at 08:28
  • I didn't assume. I suggested. – marvc1 Aug 04 '15 at 09:31
  • "So far, I have worked around the issue by grabbing the parameters in the activate() function instead." Working as intended. [Reference: Route Parameters and Query Strings](http://durandaljs.com/documentation/Using-The-Router.html). If you want the parameter to be passed directly to the controller via your hyperlink, then use the explicit URL (without the hash) and don't configure a Durandal route and module. – Brett Aug 14 '15 at 15:33

1 Answers1

0

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...

Matt
  • 6,787
  • 11
  • 65
  • 112