【问题标题】:How editable and no editable field in text box according to role根据角色,文本框中的可编辑字段和不可编辑字段如何
【发布时间】:2019-11-09 19:43:33
【问题描述】:

我在 Web 应用程序中使用 ASP.NET 我想要做的是,当您进入系统时,根据用户的不同,启用一些输入文本。例如,如果用户是管理员,则所有输入文本都被启用,但如果他们是普通用户,则所有输入文本都被禁用。

有必要记住,您使用的会话变量取决于用户的类型:

  • 会话[“typeUser”](字符串)
    • 用户
    • 管理员
    • 技术
    • 帮助台

我被咨询过,可以用这个功能完成

@HttpContext.Current.Session["typeUser"].ToString();

这是我想要的字段不可编辑的表单的代码 cshtml。在此表单中,我不希望前三个字段可编辑:注册、全名和电子邮件

<form id="InformationUser" class="contact-form">
                        <div class="box-body">
                            <div class="form-group">
                                <label for="Enrollment">Enrollment</label>
                                <input type="text" name="Enrollment" class="form-control" />
                            </div>
                            <div class="form-group">
                                <label for="Full name">Full name:</label>
                                <input type="text" name="Full name" class="form-control" />
                            </div>
                            <div class="form-group">
                                <label for="Email">Email:</label>
                                <input type="text" name="email" class="form-control" />
                            </div>
                            <div class="form-group">
                                <label for="Extension">Extension:</label>
                                <input type="text" name="extension" class="form-control">
                            </div>
                            <div class="form-group">
                                <label for="Area">Area:</label>
                                <select name="area" class="form-control select2 select2-hidden-accessible" style="width: 100%;" tabindex="-1" aria-hidden="true"></select>
                            </div>
                            <div class="form-group">
                                <label for="Ubicacion">Ubicacion:</label>
                                <select name="ubicacion" class="form-control select2 select2-hidden-accessible" style="width: 100%;" tabindex="-1" aria-hidden="true"></select>
                            </div>
                        </div>
                    </form>

【问题讨论】:

    标签: c# html asp.net .net model-view-controller


    【解决方案1】:

    在最后添加一些 javascript 来为您完成这项工作。假设您有剃刀视图引擎,这应该让您了解如何处理它:

    <script type='text/javascript'>
       var role = @HttpContext.Current.Session["typeUser"].ToString();
    
       switch (role){
        case "user" :
          document.getElementByName("Enrollment").disabled=true;  // or whatever
       break;
    
    
    }
    
    </script>
    

    【讨论】:

    • 我执行了你留给我的代码并得到一个异常 System.NullReferenceException: Object reference not established as an instance of an object。
    猜你喜欢
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    • 2011-05-26
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    相关资源
    最近更新 更多