我想将部分视图(_Test.cshtml)加载到我的产品控制器的Index视图中。Angular ng-view在MVC局部视图中不起作用
但是局部视图在新页面打开,我在这里和谷歌搜索了很多。但我似乎无法弄清楚我做错了什么?任何帮助真的很感激!
MVC控制器
public ActionResult Test()
{
return PartialView("~/Views/Product/_Test.cshtml");
}
//this returns the partial view
public ActionResult TestData()
{
List<Product> products = new List<Product>();
var db = new HotStuffPizzaContext();
var result = (from p in db.Products
select p)
.Where(x => x.ProductID != 0)
.Select(a => new
{
ProductID = a.ProductID,
Description = a.Description
});
return Json(result.ToList(), JsonRequestBehavior.AllowGet);
}
//这是通过角控制器
角脚本
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(['$routeProvider','$locationProvider', function ($routeProvider, $locationProvider) {
$routeProvider.
when('/Product', {
templateUrl: '/Product/Index',
controller: 'testCtrl'
}).
when('Product/Test', {
templateUrl: '/Views/Product/_Test',
controller: 'testCtrl'
}).otherwise({
redirectTo: '/'
});
//$locationProvider.html5Mode(true);
//$locationProvider.hashPrefix('!').html5Mode(true);
//$locationProvider.html5Mode(false).hashPrefix("!");
}]);
myApp.controller('testCtrl', ['$scope', '$http',
function ($scope, $http) {
$http.get('/Product/TestData').success(function (data) {
$scope.products = data;
});
}]);
产品/ Index.cshtml
称为@{
ViewBag.Title = "Index";
}
索引视图
<a href="Product/Test">Test</a>
<div ng-view="">
</div>
管窥
@{ Layout = null; }
<html data-ng-app="myApp">
<body data-ng-controller="testCtrl">
<div>
<table class="table table-striped">
<tr >
<th>id</th>
<th>description</th>
</tr>
<tr data-ng-repeat="p in products">
<td>{{p.ProductID}}</td>
<td>{{p.Description}}</td>
</tr>
</table>
</div>
</body>
</html>
<script src="../../Scripts/angular.min.js"></script>
<script src="../../Scripts/angular-route.js"></script>
<script src="../../Scripts/App/app.js"></script>
我也删除来自于我的应用包罗万象开始routeconfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "product",
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "product",
// url: "{product}/{*catchall}",
// defaults: new { controller = "Product", action = "Index"});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }) ;
}
}
您是否正在尝试将角度应用加载到您的MVC应用程序的容器中?即索引是不是一个角度的应用程序,但部分将是儿童角度的应用程序?或者这是一个MVC后端角度的应用程序和索引页面始终配置为角? – Jasen
角度应用程序有一个mvc后端。因此,mvc将为每个控制器提供数据和顶层视图。但是Product/angular路径之后的任何东西都应该接管并在ng视图中显示部分html – Haris
不删除CATCH ALL使MVC完成工作(而不是Angular)? –