路由到不同的视图(使用@ Html.ActionLink和视图NG-视图)在Asp.Net MVC与AngularJS,NG-视图只工作一次,位置URL奇怪视图、路由、奇怪、不同

由网友(、请叫我上帝)分享简介:在我而言,我需要使用 @ Html.ActionLink 路由到不同的视图,在asp.net AngularJS.In各厂景/ index.cshtml具有< D​​IV NG-视图>< / DIV> ,将其加载HTML模板In my case, I need use @Html.Action...

在我而言,我需要使用 @ Html.ActionLink 路由到不同的视图,在asp.net AngularJS.In各厂景/ index.cshtml具有< D​​IV NG-视图>< / DIV> ,将其加载HTML模板

In my case, I need use @Html.ActionLink to Route to different View with AngularJS.In each view1/index.cshtml in asp.net has <div ng-view></div> that will load its html template.

下面链接的图像是我的项目结构:结果 点击

the following link image is my project structure: click

我的问题是:它只航线成功第一再寄一次觉得它会导致错误的位置的URL。

My problem is: it only route success first time.I think it cause wrong location url.

浏览/分享/ _Layout.cshtml:

<ul>
    <li>@Html.ActionLink("Home", "Index", "Home")</li>
    <li>@Html.ActionLink("Device Management", "Index", "DeviceManagement")</li>
</ul>

查看/主页/ index.cshtml:

@{
    ViewBag.Title = "Home Page";
    ViewBag.InitModule = "homeIndex";
}
@section Scripts {
    <script src="~/app/home/homeIndex.js"></script>
}
<div ng-view><div>

应用的/ home / homeIndex.js

var homeIndex = angular.module('homeIndex', ["ngRoute"]);

homeIndex.config(["$routeProvider", function($routeProvider){
    $routeProvider
        .when("/home",{
            templateUrl:"app/home/homeIndex.html",
            controller:"homeCtrl"

        })

        .otherwise({
            redirectTo:"/home"
        });
}]);

应用的/ home / homeIndex.html

<a href="#/home2">HOME2</a>

应用/ deviceManagement / deviceManageIndex.js

var deviceManageIndex = angular.module('deviceManageIndex', ["ngRoute"]);

deviceManageIndex.config(["$routeProvider", function($routeProvider) {
    $routeProvider
        .when("/device", {
            templateUrl: "app/deviceManagement/deviceManagementIndex.html",
            controller: "deviceManageCtrl"

        })
        .otherwise({
            redirectTo:"/device"
        });

}]);

结果

该网站有侧边栏的首页的和的设备管理的在侧边栏按钮。

the web site has sidebar, Home and Device Management Buttons in the sidebar.

当我点击的首页的,它会重定向正确的位置,它可以加载homeIndex.html成功的:

when I click Home, it will redirect right location, it can load homeIndex.html successful:

位置是:

http://localhost:13060/#/home

但是当我点击的首页的按钮第二次,它不能加载homeIndex.html了。

but when I click Home Button second time, it can't load homeIndex.html anymore.

位置将变为:

http://localhost:13060/#

同样的问题发生在另一个按钮的设备管理的

当我点击的设备管理的,它会重定向正确的位置,它可以加载deviceManagementIndex.html成功的:

when I click Device Management, it will redirect right location, it can load deviceManagementIndex.html successful:

位置是:

http://localhost:13060/DeviceManagement#/device

但是当我点击的设备管理的按钮第二次,它不能加载deviceManagementIndex.html了。

but when I click Device Management Button second time, it can't load deviceManagementIndex.html anymore.

位置将变为:

http://localhost:13060/DeviceManagement#

我觉得位置是很奇怪的,在第一次,它应该是:

I think the location is quite strange,in first time,it should be :

http://localhost:13060/DeviceManagement/#/device

我想这是最主要的原因不加载HTML模板正确...

I guess that is main reason not to load html template correctly...

但是,为什么位置URL不能正确显示,请帮我解决这个头痛的问题....

But why the location url can't show correctly, Please Help me solve the headache problem....

推荐答案

感谢肖恩提示!对我来说,我应该在客户端使用角路由, $ routeProvider templateUrl 来触发施维雅侧控制器加载服务器端查看,这是我找到的解决方案,这样ActionLink的行为,可以通过服务器端,它会跟服务器端的路由规则来选择相应的视图。这是768,16 ...工作:D

Thanks to Shawn tip!In my case, I should use angular routing in client side, $routeProvider with templateUrl to trigger servier side controller to load server side View,That's I found solution, in this way actionlink behavior could through server side, it will follow server side route rule to chose appropriate View. It shoud be work...:D

阅读全文

相关推荐

最新文章