如何将一个值从MVC控制器传递给Angular视图

本文关键字:控制器 MVC 视图 Angular 一个 | 更新日期: 2023-09-27 18:07:10

我使用angularjs与Asp.net MVC来检查用户文件夹的写访问权限。如果用户有写权限,那么我想显示一个div,其中有一个链接。我有一个div在SampleView.Html,我有一个方法检查用户的写访问在MVC控制器称为ReportController.cs。我可以用什么代码来将值从MVC控制器传递到Angularjs视图?

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

如何将一个值从MVC控制器传递给Angular视图

我可能没有完全正确地理解这个问题,但是如果您需要,最好使用razor语法显示或隐藏它,而不是暴露变量并在JavaScript中处理它。如果您没有从ASP返回Model。. 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的顶部声明它为true。网络控制器。