如何使用angularjs在mvc中进行crud操作?
问题描述:
请帮我在mvc中创建一个带有angularjs的应用程序,它支持插入,更新和删除。
注意:我不需要web api
Please help me to create an application in mvc with angularjs which supports insert,update and delete.
Note: I don't need web api
答
尝试
使用ASP.NET Web API和Angular进行CRUD操作。 js [ ^ ]
CRUD with SPA,ASP .NET Web API和Angular.js [ ^ ]
使用MVC Web API的AngularJS(ASP.NET MVC RESTful服务) [ ^ ]
Try
CRUD Operations using ASP.NET Web API and Angular.js [^]
CRUD with SPA, ASP.NET Web API and Angular.js[^]
AngularJS With MVC Web API (ASP.NET MVC RESTful Service)[^]
表面上你可以像使用WebAPI一样使用MVC,MVC只是有更多的开销,并提供内置的支持,如捆绑等漂亮的东西。 MVC也只是想要特定的函数调用,而不是使用HttpMethod来路由流量。
这意味着你需要调用Angular中的特定函数
Ostensibly you can use MVC just like WebAPI, MVC just has more overhead and offers built-in support for nifty things like bundling. MVC also just wants specific function calls rather than using the HttpMethod to route traffic.
This means that you need to call specific functions from your Angular
资源,并且您需要多个服务来完成一个角度/ webAPI服务的工作。没什么大不了的,只是你需要注意的事情。
为了在这样的SPA应用中干净地使用MVC(按预期),使用标准模型,但返回Json而不是views / partials:
resource, and that you'll need multiple services to do the job of one angular/webAPI service. Not a big deal, just something that you need to be aware of.
In order to cleanly use MVC (as intended) for SPA applications like this, use the standard model, but return Json instead of views/partials:
public class CrudController
{
public ActionResult ReadMyEntity(int id)
{
var json = dataContext.MyEntities.Where(x => x.Id == id).ToList();
return Json(json);
}
}
这将为你的Angular端点提供一个Json数组,从这一点你可以使用它就像任何其他类型的资源一样。
This will provide a Json array to your Angular endpoint, from which point you can use it just like any other sort of resource.