Programmermid Programmermid - 1 year ago 97
AngularJS Question

How to pass a Value from MVC Controller to Angular .html View

I am using angularjs with MVC to check the write access of a folder for users. If the user has the write access then I want to show a div which has a link. I have a Div in SampleView.Html and I have a method which checks for user's write access in MVC Controller called ReportController.cs. what will be the code for Angular Controller that I can use to pass value from MVC controller to Angularjs View?


<div id="DivPackerTemplate" class="cp-btn cp-btn-primary pull-right"><a ng-href="\\Samplefolder" >Edit Template</a></div>


public void AccessPackerPlanTemplate(string folderPath)
string path = @"\\sample";
string NtAccountName = @"sampleuser";

DirectoryInfo di = new DirectoryInfo(path);
DirectorySecurity acl = di.GetAccessControl(AccessControlSections.All);
AuthorizationRuleCollection rules = acl.GetAccessRules(true, true, typeof(NTAccount));

//Go through the rules returned from the DirectorySecurity
foreach (AuthorizationRule rule in rules)
//If we find one that matches the identity we are looking for
if (rule.IdentityReference.Value.Equals(NtAccountName, StringComparison.CurrentCultureIgnoreCase))
//Cast to a FileSystemAccessRule to check for access rights
if ((((FileSystemAccessRule)rule).FileSystemRights & FileSystemRights.WriteData) > 0)
//Show the link
DivPackerTemplate.Visible = false; \\This is not working is there a alternative for this?

Answer Source

If your using angular you should make SampleView.Html a directive and inject a service that can call your mvc AccessPackerPlanTemplate method to get the information or better yet create an angular rule service that can wrap and all your rule logic and cache results.

Step 1: create the directive to wrap DivPackerTemplate Directive doc ->

Step 2: create an angular service that wraps your calls to rules logic (rules should be in webapi but you can use regular mvc actions if you must) ->

Then you simply wire them up by injecting your rules service into the directive and using it to populate the template data, caching is optional.

In the angular world you do not "pass value's" from the server into a view, your views use angular services to "pull values" into views/directives

I'm not an angular expert but something along these lines, omitted the rules service implementation ($rules) as angular services are fairly easy to create.

angular.module('moduleA', [])
.controller('SimpleDirectiveController', ['$rules','$scope',function($rules,$scope) { 
$ = function() { 
    return $rules.yourmethodtogetrulesresult();//for this case return either 'hidden' or 'visible'
}; }])
.directive('PackerTemplate', function() {
  return {
    template: function($scope) { 
        var templatevar =  '<a ng-href="\\Samplefolder" visibility=\'[XX]\'>Edit Template</a>'
         return templatevar.replace('[XX]',$
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download