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.

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Inside foreach loop had to use "Model".
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)
{
    ...
}

Comments

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 
 }

2 Comments

This way it is showing System.String[] as output on view page.
You'll have to iterate through the list i have updated my answer check this post for displaying the ViewBag stackoverflow.com/questions/10521831/…

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.