Programmermid
Programmermid

Reputation: 598

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

I am using angularjs with Asp.net 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?

SampleView.html:

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

ReportController.cs:

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?
                            }
                        }
                    }
                }

Upvotes: 1

Views: 2132

Answers (2)

Paul Swetz
Paul Swetz

Reputation: 2254

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 -> https://docs.angularjs.org/guide/directive

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) -> https://docs.angularjs.org/guide/services

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) { 
$scope.show = 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]',$scope.show())
         }     
    };
});

Upvotes: 0

Law
Law

Reputation: 369

From your MVC application you can return an asp view. Inside the view you can use your angular controller like this:

@model dynamic

@{
    Layout = "";
}

<div ng-controller="PackerListController as vm">
   <h1 class="page-header">
     <button ng-click="vm.editTemplate()">Edit Template</button>
   </h1>
</div>

Upvotes: 0

Related Questions