0
  public ActionResult About()
    {
        var roles = System.Web.Security.Roles.GetAllRoles();            

        return View();
    }

I don't know how to take this string on view page. Please help me.

Smoking monkey
  • 323
  • 1
  • 6
  • 22

3 Answers3

4

You should have your view accept a string[] Model and pass this model from your controller to your view like this:

public ActionResult About()
{
  var model = System.Web.Security.Roles.GetAllRoles();                   
  return View(model);
}

In your view you'd have something like this (assuming you are using the Razor ViewEngine):

@model string[]

<ul>
@foreach(var role in model) 
{
   <li>@role</li>
}
</ul>
Mats
  • 14,902
  • 33
  • 78
  • 110
1

You can set a ViewBag

public ActionResult About()
{
      ViewBag.roles = System.Web.Security.Roles.GetAllRoles();                   
      return View();
}

and u can access this ViewBag object on the page by @ViewBag.roles

To display the list

foreach(var customrole in ViewBag.roles)
 {
    @customrole.Roles // This might be some property you need to display 
 }
Nikitesh
  • 1,287
  • 1
  • 17
  • 38
  • This way it is showing System.String[] as output on view page. – Smoking monkey Jun 05 '14 at 08:37
  • You'll have to iterate through the list i have updated my answer check this post for displaying the ViewBag http://stackoverflow.com/questions/10521831/how-to-display-a-list-using-viewbag – Nikitesh Jun 05 '14 at 08:53
1

The View method takes a model which can be your string[].

public ActionResult About()
{
    var roles = System.Web.Security.Roles.GetAllRoles();

    return View(roles);
}

Then your view would look something like this

@model System.Array

@foreach (var role in Model)
{
    ...
}
heymega
  • 9,215
  • 8
  • 42
  • 61