C# Angular和ASP.NET MVC
我正在学习angular,我正在使用ASP.NET MVC5。我无法通过angular从mvc控制器返回数据 路由应该是localhost/Product/Test,但是当angular get方法调用它时,url是locahost/Product/Product/Test。欢迎任何帮助 我采取的方法也正确吗?因为我看过很多教程,它们展示了如何只返回视图。但似乎无法找到如何返回视图和数据 屏幕截图 MVC控制器C# Angular和ASP.NET MVC,c#,asp.net,angularjs,asp.net-mvc,C#,Asp.net,Angularjs,Asp.net Mvc,我正在学习angular,我正在使用ASP.NET MVC5。我无法通过angular从mvc控制器返回数据 路由应该是localhost/Product/Test,但是当angular get方法调用它时,url是locahost/Product/Product/Test。欢迎任何帮助 我采取的方法也正确吗?因为我看过很多教程,它们展示了如何只返回视图。但似乎无法找到如何返回视图和数据 屏幕截图 MVC控制器 public ActionResult Test() {
public ActionResult Test()
{
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 View("~/Views/Product/_Test.cshtml", result);
}
局部视图
<html data-ng-app="myApp">
<head>
<title></title>
<script src="../../Scripts/angular.min.js"></script>
<script src="../../Scripts/angular-route.js"></script>
<script src="../../Scripts/App/app.js"></script>
</head>
<body data-ng-controller="testCtrl">
<div>
<table class="table table-striped">
<tr data-ng-repeat="p in products">
<th>id</th>
<th>description</th>
</tr>
<tr>
<td>{{p.productid}}</td>
<td>{{p.description}}</td>
</tr>
</table>
</div>
</body>
</html>
<h1>INDEX VIEW</h1>
<div data-ng-view=""></div>
<a href="/Product/Test">Test</a>
<script src="../../Scripts/angular.min.js"></script>
<script src="../../Scripts/angular-route.js"></script>
<script src="../../Scripts/App/app.js"></script>
身份证件
描述
{{p.productid}}
{{p.description}}
索引视图
<html data-ng-app="myApp">
<head>
<title></title>
<script src="../../Scripts/angular.min.js"></script>
<script src="../../Scripts/angular-route.js"></script>
<script src="../../Scripts/App/app.js"></script>
</head>
<body data-ng-controller="testCtrl">
<div>
<table class="table table-striped">
<tr data-ng-repeat="p in products">
<th>id</th>
<th>description</th>
</tr>
<tr>
<td>{{p.productid}}</td>
<td>{{p.description}}</td>
</tr>
</table>
</div>
</body>
</html>
<h1>INDEX VIEW</h1>
<div data-ng-view=""></div>
<a href="/Product/Test">Test</a>
<script src="../../Scripts/angular.min.js"></script>
<script src="../../Scripts/angular-route.js"></script>
<script src="../../Scripts/App/app.js"></script>
索引视图
更新首先将中的“产品/测试”替换为“/Product/Test”注意开头的“/”
您的操作不会要求返回json数据,而是向您的Angular应用程序发送一个视图(将是纯HTML)
您必须在测试操作中返回Json数据。替换以下行:
return View("~/Views/Product/_Test.cshtml");
通过这一行:
return Json(result.ToList());
您可能需要允许AJAX请求使用GET
方法,然后使用:
return Json(result.ToList(), JsonRequestBehavior.AllowGet);
不要忘记将[GET]
属性添加到测试操作中
您必须考虑使用ASP.NETWebAPI而不是ASP.NETMVC
更新1:根据下面的评论回答:
通过正确重命名匿名类型替换linq查询,如下所示:
var result = (from p in db.Products
select p)
.Where(x => x.ProductID != 0)
.Select(a => new
{
productid = a.ProductID,
description = a.Description
});
请注意,匿名类型属性中没有大写字母。您必须这样使用它,因为您的AngularJS应用程序需要它:
<tr>
<td>{{p.productid}}</td>
<td>{{p.description}}</td>
</tr>
您的TestData
liek如下:
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);
}
public ActionResult TestData()
{
列表产品=新列表();
var db=新的HotStuffPizzaContext();
var结果=(来自数据库产品中的p
选择p)
.Where(x=>x.ProductID!=0)
.选择(a=>新建
{
productid=a.productid,
描述
});
返回Json(result.ToList(),JsonRequestBehavior.AllowGet);
}
最后,将
$http.get('Product/Test')
的url替换为$http.get('/Product/TestData')
尝试从索引视图中的链接中删除/Product。您的控制器名称是什么?(包含您的测试操作的类。@RickestRick我在“/”应用程序中遇到以下服务器错误。URL是@CodeNotFound Product ControllerIn$http.get('Product/Test')。我刚刚尝试了这个,它将我带到json[{“ProductID”:1,“Description”:“Margarita”},{“ProductID”:2,“Description”:“Pepperoni”},{“ProductID”:3,“Description”的结果):“Burger”}]它没有效果,仍然得到相同的结果,我已经更新了问题并包含了一个屏幕快照。它不起作用,它只返回我的屏幕快照中的内容。在调试器中,通过单击索引视图中的href,我看不到任何加载到页面号中的脚本。产品控制器现在正在执行返回Json(result.ToList(),JsonRequestBehavior.AllowGet);(这就是导致这样做的原因)我已经尝试过了,但仍然得到了相同的结果。:z