如何将值从MVC控制器传递到角度视图

时间:2016-09-14 17:11:53

标签: c# angularjs asp.net-mvc razor

我正在使用带有Asp.net MVC的angularjs来检查用户对文件夹的写访问权限。如果用户具有写访问权限,那么我想显示具有链接的div。我在SampleView.Html中有一个div,我有一个方法可以在MVC Controller中检查用户的写访问权限ReportController.cs。 Angular Controller的代码是什么,我可以使用它将值从MVC控制器传递给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?
                            }
                        }
                    }
                }

1 个答案:

答案 0 :(得分:0)

我可能无法正确理解这个问题,但我认为最好只使用剃刀语法显示或隐藏它,而不是暴露变量并在JavaScript中处理它。如果您没有从ASP.NET控制器返回模型,那么在ASP.NET控制器中设置ViewBag变量,如下所示:

ViewBag.DivPackerTemplateVisible = false;

在你看来:

@if(ViewBag.DivPackerTemplateVisible) {
    <div id="DivPackerTemplate" class="cp-btn cp-btn-primary pull-right"><a ng-href="\\Samplefolder" >Edit Template</a></div>

}

您还应确保ViewBag.DivPackerTemplateVisible的值始终为true或false,可能是在ASP.NET控制器顶部将其声明为true。