Durandal没有将参数传递给MVC控制器动作(Durandal not passing parameters to MVC controller action)

编程入门 行业动态 更新时间:2024-10-26 10:40:35
Durandal没有将参数传递给MVC控制器动作(Durandal not passing parameters to MVC controller action)

我在Durandal中使用ASP.NET MVC应用程序中的参数时遇到问题...

我使用MVC路由属性 ,例如: [Route("admin/pages")] , [Route("admin/blog")]等。

我将Durandal整合如下:

main.js:

app.start().then(function() { viewEngine.viewExtension = '/'; viewLocator.convertModuleIdToViewId = function (moduleId) { return moduleId.replace('viewmodels', ''); }; app.setRoot('viewmodels/admin/shell', 'entrance'); });

这很好用,因为viewId最终匹配MVC路由。

但是,我现在面临着尝试在路由中使用参数的严重问题。 例如,我有一条路线如下:

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

而我的Durandal路线是:

localization/localizable-strings/:languageId

示例锚标记:

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

这不起作用,因为看起来Durandal没有将参数传递给服务器。 它只是从/admin/localization/localizable-strings检索HTML,它返回一个404页面,因为没有参数传递给MVC操作。

我有什么选择?

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?

最满意答案

我知道这个问题现在已经快2年了,但我显然忘了用当时的解决方案关闭它。 基本上,Brett和Marvin是正确的,但由于我当时对Durandal(以及一般的SPA)这么新,我误解了他们告诉我的确切内容。 我结束了这样做:

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

而durandal路线的设置如下;

"localization/localizable-strings/:cultureCode"

然后我将使用该参数通过ajax获取相关数据。 我的错误是希望参数能够在MVC操作方法中使用,这样我就可以获得一些数据服务器端,这当然不起作用......

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

更多推荐

admin,MVC,Durandal,localizable-strings,参数,电脑培训,计算机培训,IT培训"/> <met

本文发布于:2023-07-23 01:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225593.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   参数   动作   MVC   Durandal

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!