1

I am using mvc in which I have two type of users; "student" and "staff". I want to filter these users and show a menu to them which, depending whether the user is a student or staff, should change. I'm trying to do this with Javascript.

How do I accomplish this?

This is what I already have:

<strong>STAFF USER</strong>

<div id="menucontainer">
    <ul id="menu">                  
        <li>@Html.ActionLink("Staff Details", "StaffDetails", "Home")</li>
        <li>@Html.ActionLink("Collection Master", "CollectionMaster", "Home")</li>
        <li>@Html.ActionLink("Question Bank Master", "QuestionBankMaster", "Home")</li>
        <li>@Html.ActionLink("Student Details", "StudentPersonnelDetails", "Home")</li>
        <li>@Html.ActionLink("Answer Sheet", "Answersheet", "Home")</li>
        <li>@Html.ActionLink("Login", "Login", "Home")</li>
        <li>@Html.ActionLink("Exam Master", "ExamMaster", "Home")</li>
        <li>@Html.ActionLink("Assign", "Assign", "Home")</li>
    </ul>
</div>

<strong>STUDENT USER</strong>

<div id="menucontainer1">
    <ul id="menu1">                  
        <li>@Html.ActionLink("Student Details", "StudentPersonnelDetails", "Home")</li>
        <li>@Html.ActionLink("Answer Sheet", "Answersheet", "Home")</li>
    </ul>
</div>
2
  • Do you want to hide it on client or server side?
    – yan.kun
    Commented Jul 3, 2013 at 8:00
  • 2
    Well, he said Javascript so... Edit: though I'd argue flagging it server side makes more sense. Commented Jul 3, 2013 at 8:08

1 Answer 1

2

Straightforward example:

<script type="text/javascript">
  function Foo()
  {
       if(@Roles.GetRolesForUser().Contains("Staff"))
       {
           document.getElementById('menucontainer').style.display = 'block';
           document.getElementById('menucontainer1').style.display = 'none';
       }
       else
       {
          document.getElementById('menucontainer').style.display = 'none';
          document.getElementById('menucontainer1').style.display = 'block';
       }
  }
</script>
3
  • 1
    And then call the function, or not have it in a function at all.
    – Jonathan
    Commented Jul 3, 2013 at 8:13
  • 2
    I'm pretty sure the OP is capable of calling a function
    – DGibbs
    Commented Jul 3, 2013 at 8:15
  • Me too. I just wanted to make it explicit in case someone didn't know. Sorry if I came across as rude. :)
    – Jonathan
    Commented Jul 3, 2013 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.