Smoking monkey
Smoking monkey

Reputation: 323

How to print the string array on view page in Asp.net Mvc?

  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.

Upvotes: 0

Views: 10980

Answers (3)

Nikitesh
Nikitesh

Reputation: 1305

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 
 }

Upvotes: 1

Mats
Mats

Reputation: 14817

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>

Upvotes: 4

heymega
heymega

Reputation: 9401

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)
{
    ...
}

Upvotes: 1

Related Questions