Skip to main content
adicionou 133 caracteres ao conteúdo
Fonte Link
user6026
user6026

Para tal recuperação na View, como ela tem um tipo definido, você da um CAST para o especificado tipo que ai você consegue trabalhar de maneira ideal com os elementos dessa List de KeyValuePair.

Com essa lista seria assim:

1 ) List

No código

public ActionResult Index()
{
    List<KeyValuePair<string, string>> lista = new List<KeyValuePair<string, string>>();
    lista.Add(new KeyValuePair<string, string>("item1", "value1"));
    lista.Add(new KeyValuePair<string, string>("item2", "value2"));

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;
}
@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor com Linq dependendo da chave

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;

    string value = "";
    KeyValuePair<string, string> saida;
    saida = lista.ToList().Where(x => x.Key == "item1").FirstOrDefault();
    if (saida.Key != null && saida.Value != null)
    {
        value = saida.Value;
    }    
}

2 ) Dictionary (recomendo)

No código:

public ActionResult Index()
{
    Dictionary<string, string> lista = new Dictionary<string, string>();            
    lista.Add("item1", "value1");
    lista.Add("item2", "value2");

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    Dictionary<string, string> lista = (Dictionary<string, string>)ViewBag.WebConfigPermissao;
}

@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor conforme chave

String saida;
lista.TryGetValue("item1", out saida);

Referencias:

Com essa lista seria assim:

1 ) List

No código

public ActionResult Index()
{
    List<KeyValuePair<string, string>> lista = new List<KeyValuePair<string, string>>();
    lista.Add(new KeyValuePair<string, string>("item1", "value1"));
    lista.Add(new KeyValuePair<string, string>("item2", "value2"));

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;
}
@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor com Linq dependendo da chave

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;

    string value = "";
    KeyValuePair<string, string> saida;
    saida = lista.ToList().Where(x => x.Key == "item1").FirstOrDefault();
    if (saida.Key != null && saida.Value != null)
    {
        value = saida.Value;
    }    
}

2 ) Dictionary (recomendo)

No código:

public ActionResult Index()
{
    Dictionary<string, string> lista = new Dictionary<string, string>();            
    lista.Add("item1", "value1");
    lista.Add("item2", "value2");

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    Dictionary<string, string> lista = (Dictionary<string, string>)ViewBag.WebConfigPermissao;
}

@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor conforme chave

String saida;
lista.TryGetValue("item1", out saida);

Referencias:

Para tal recuperação na View, como ela tem um tipo definido, você da um CAST para o especificado tipo que ai você consegue trabalhar de maneira ideal com os elementos dessa List de KeyValuePair.

Com essa lista seria assim:

1 ) List

No código

public ActionResult Index()
{
    List<KeyValuePair<string, string>> lista = new List<KeyValuePair<string, string>>();
    lista.Add(new KeyValuePair<string, string>("item1", "value1"));
    lista.Add(new KeyValuePair<string, string>("item2", "value2"));

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;
}
@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor com Linq dependendo da chave

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;

    string value = "";
    KeyValuePair<string, string> saida;
    saida = lista.ToList().Where(x => x.Key == "item1").FirstOrDefault();
    if (saida.Key != null && saida.Value != null)
    {
        value = saida.Value;
    }    
}

2 ) Dictionary (recomendo)

No código:

public ActionResult Index()
{
    Dictionary<string, string> lista = new Dictionary<string, string>();            
    lista.Add("item1", "value1");
    lista.Add("item2", "value2");

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    Dictionary<string, string> lista = (Dictionary<string, string>)ViewBag.WebConfigPermissao;
}

@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor conforme chave

String saida;
lista.TryGetValue("item1", out saida);

Referencias:

adicionou 133 caracteres ao conteúdo
Fonte Link
user6026
user6026

Com essa lista seria assim:

1 ) List

No código

public ActionResult Index()
{
    List<KeyValuePair<string, string>> lista = new List<KeyValuePair<string, string>>();
    lista.Add(new KeyValuePair<string, string>("item1", "value1"));
    lista.Add(new KeyValuePair<string, string>("item2", "value2"));

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;
}
@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor com LinqLinq dependendo da chave

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;

    string value = "";
    KeyValuePair<string, string> saida;
    saida = lista.ToList().Where(x => x.Key == "item1").FirstOrDefault();
    if (saida.Key != null && saida.Value != null)
    {
        value = saida.Value;
    }    
}

2 ) DictionaryDictionary (recomendo)

No código:

public ActionResult Index()
{
    Dictionary<string, string> lista = new Dictionary<string, string>();            
    lista.Add("item1", "value1");
    lista.Add("item2", "value2");

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    Dictionary<string, string> lista = (Dictionary<string, string>)ViewBag.WebConfigPermissao;
}

@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor conforme chave

String saida;
lista.TryGetValue("item1", out saida);

Referencias:

Com essa lista seria assim:

1 ) List

No código

public ActionResult Index()
{
    List<KeyValuePair<string, string>> lista = new List<KeyValuePair<string, string>>();
    lista.Add(new KeyValuePair<string, string>("item1", "value1"));
    lista.Add(new KeyValuePair<string, string>("item2", "value2"));

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;
}
@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor com Linq dependendo da chave

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;

    string value = "";
    KeyValuePair<string, string> saida;
    saida = lista.ToList().Where(x => x.Key == "item1").FirstOrDefault();
    if (saida.Key != null && saida.Value != null)
    {
        value = saida.Value;
    }    
}

2 ) Dictionary (recomendo)

No código:

public ActionResult Index()
{
    Dictionary<string, string> lista = new Dictionary<string, string>();            
    lista.Add("item1", "value1");
    lista.Add("item2", "value2");

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    Dictionary<string, string> lista = (Dictionary<string, string>)ViewBag.WebConfigPermissao;
}

@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor conforme chave

String saida;
lista.TryGetValue("item1", out saida);

Com essa lista seria assim:

1 ) List

No código

public ActionResult Index()
{
    List<KeyValuePair<string, string>> lista = new List<KeyValuePair<string, string>>();
    lista.Add(new KeyValuePair<string, string>("item1", "value1"));
    lista.Add(new KeyValuePair<string, string>("item2", "value2"));

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;
}
@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor com Linq dependendo da chave

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;

    string value = "";
    KeyValuePair<string, string> saida;
    saida = lista.ToList().Where(x => x.Key == "item1").FirstOrDefault();
    if (saida.Key != null && saida.Value != null)
    {
        value = saida.Value;
    }    
}

2 ) Dictionary (recomendo)

No código:

public ActionResult Index()
{
    Dictionary<string, string> lista = new Dictionary<string, string>();            
    lista.Add("item1", "value1");
    lista.Add("item2", "value2");

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    Dictionary<string, string> lista = (Dictionary<string, string>)ViewBag.WebConfigPermissao;
}

@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor conforme chave

String saida;
lista.TryGetValue("item1", out saida);

Referencias:

adicionou 133 caracteres ao conteúdo
Fonte Link
user6026
user6026

Com essa lista seria assim:

1 ) List

No código

public ActionResult Index()
{
    List<KeyValuePair<string, string>> lista = new List<KeyValuePair<string, string>>();
    lista.Add(new KeyValuePair<string, string>("item1", "value1"));
    lista.Add(new KeyValuePair<string, string>("item2", "value2"));

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;
}
@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor com Linq dependendo da chave

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;

    string value = "";
    KeyValuePair<string, string> saida;
    saida = lista.ToList().Where(x => x.Key == "item1").FirstOrDefault();
    if (saida.Key != null && saida.Value != null)
    {
        value = saida.Value;
    }    
}

2 ) Dictionary (recomendo)

No código:

public ActionResult Index()
{
    Dictionary<string, string> lista = new Dictionary<string, string>();            
    lista.Add("item1", "value1");
    lista.Add("item2", "value2");

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    Dictionary<string, string> lista = (Dictionary<string, string>)ViewBag.WebConfigPermissao;
}

@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor conforme chave

String saida;
lista.TryGetValue("item1", out saida);

Com essa lista seria assim:

1 ) List

No código

public ActionResult Index()
{
    List<KeyValuePair<string, string>> lista = new List<KeyValuePair<string, string>>();
    lista.Add(new KeyValuePair<string, string>("item1", "value1"));
    lista.Add(new KeyValuePair<string, string>("item2", "value2"));

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;
}
@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor com Linq dependendo da chave

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;

    KeyValuePair<string, string> saida;
    saida = lista.ToList().Where(x => x.Key == "item1").FirstOrDefault();
    
}

2 ) Dictionary (recomendo)

No código:

public ActionResult Index()
{
    Dictionary<string, string> lista = new Dictionary<string, string>();            
    lista.Add("item1", "value1");
    lista.Add("item2", "value2");

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    Dictionary<string, string> lista = (Dictionary<string, string>)ViewBag.WebConfigPermissao;
}

@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor conforme chave

String saida;
lista.TryGetValue("item1", out saida);

Com essa lista seria assim:

1 ) List

No código

public ActionResult Index()
{
    List<KeyValuePair<string, string>> lista = new List<KeyValuePair<string, string>>();
    lista.Add(new KeyValuePair<string, string>("item1", "value1"));
    lista.Add(new KeyValuePair<string, string>("item2", "value2"));

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;
}
@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor com Linq dependendo da chave

@{
    ViewBag.Title = "Home Page";
    List<KeyValuePair<string, string>> lista = (List<KeyValuePair<string, string>>)ViewBag.WebConfigPermissao;

    string value = "";
    KeyValuePair<string, string> saida;
    saida = lista.ToList().Where(x => x.Key == "item1").FirstOrDefault();
    if (saida.Key != null && saida.Value != null)
    {
        value = saida.Value;
    }    
}

2 ) Dictionary (recomendo)

No código:

public ActionResult Index()
{
    Dictionary<string, string> lista = new Dictionary<string, string>();            
    lista.Add("item1", "value1");
    lista.Add("item2", "value2");

    ViewBag.WebConfigPermissao = lista;


    return View();
}

Na View

@{
    ViewBag.Title = "Home Page";
    Dictionary<string, string> lista = (Dictionary<string, string>)ViewBag.WebConfigPermissao;
}

@foreach (var item in lista)
{
    @item.Key @item.Value
}

Pegar valor conforme chave

String saida;
lista.TryGetValue("item1", out saida);
adicionou 355 caracteres ao conteúdo
Fonte Link
user6026
user6026
Carregando
adicionou 97 caracteres ao conteúdo
Fonte Link
user6026
user6026
Carregando
Fonte Link
user6026
user6026
Carregando