Reputation: 7476
Say I have several Controllers, including a BaseControlelr
and I want to do some queries and send the data to my Index.cshtml
, but I'm not changing the url since this is a single page application. Normally I'd define a Route for my Get
request and retrieve it using Angular $http.get
, but is there any other way I can pass the model directly from my controller to my view?
Upvotes: 0
Views: 520
Reputation: 1039498
but is there any other way I can pass the model directly from my controller to my view?
Of course that there is. In ASP.NET MVC a controller action can return a view model to a strongly typed view:
public ActionResult Index()
{
MyViewModel model = ...
return View(model);
}
and in your strongly typed view you could just serialize this view model as an Angular constant
available to your Angular application:
@model MyViewModel
<script>
angular.module('myApp').constant('model', @Html.Raw(Json.Encode(Model)));
</script>
Now in your Angular application you can play with this model
variable:
angular.module('myApp').controller('MainCtrl', ['$scope', 'model', function($scope, model) {
$scope.model = model;
}]);
And in the corresponding template:
<div ng-controller="MainCtrl">
Hello {{FirstName}} {{LastName}}
</div>
That's of course the initial bootstrapping of your SPA. From there on you might need to use pure client side techniques to work with the model.
Upvotes: 1