How to use a tuple in an asp.net mvc view?

12,985

Solution 1

You can access to components of tuple by the Item1, ...ItemN properties.

Try this:

@foreach (var module in Model.Item2)

Solution 2

you can set the @model to be a dynamic object, and casting it to Tuple object.

@model dynamic 

 @{
   var result= model as Tuple<string,List<xx.Models.GlobalAdmin.Models.Modulo>;
 }

Solution 3

Tuple does not have a parameterless constructor which might be what's causing this issue. Why don't you create a single viewmodel that has a string and a list and pass it to the view? Ref MVC Custom ViewModel and auto binding

Share:
12,985
Luis Valencia
Author by

Luis Valencia

Updated on June 17, 2022

Comments

  • Luis Valencia
    Luis Valencia almost 2 years

    I have the following controller:

     public class ModuleController : Controller
        {
            // GET: Modulo
            public ActionResult GetModules()
            {
                return PartialView(Module.GetModulesForUser(ClaimsPrincipal.Current.Identities.First().Name));
            }
        }
    

    and this returns the modules for a user

      /// <summary>
            /// Gets the modules activated for a user
            /// </summary>
            /// <returns>List of modules for the selected user</returns>
            public static Tuple<string, List<Modulo>> GetModulesForUser(string identityname)
            {
                // It needs to be cached for every user because every user can have different modules enabled.
                var cachekeyname = "ApplicationModulesPerUser|" + identityname;
    
                var cache = CacheConnectionHelper.Connection.GetDatabase();
                Tuple<string, List<Modulo>> modulosPorUsuarioDeDirectorioActivo;
    
                //get object from cache
                string modulosUsuariosString = cache.StringGet(cachekeyname);
                //string modulosUsuariosString;
                if (!string.IsNullOrEmpty(modulosUsuariosString))
                {
                    modulosPorUsuarioDeDirectorioActivo = JsonConvert.DeserializeObject<Tuple<string, List<Modulo>>>(modulosUsuariosString);
                    return modulosPorUsuarioDeDirectorioActivo;
                }
    
                var extPropLookupNameModulos = $"extension_{SettingsHelper.ClientId.Replace("-", "")}_{"Modulos"}";
                var client = AuthenticationHelper.GetActiveDirectoryClient();
                var user = client.Users.GetByObjectId(identityname).ExecuteAsync().Result;
                var userFetcher = (User)user;
                var unitOfWork = new UnitOfWork();
                var keyvaluepairModulos = userFetcher.GetExtendedProperties().FirstOrDefault(prop => prop.Key == extPropLookupNameModulos);
                var idsModulos = keyvaluepairModulos.Value.ToString().Split(',');
                var listaModulos= idsModulos.Select(idModulo => unitOfWork.ModuloRepository.GetById(Convert.ToInt32(idModulo))).ToList();
                modulosPorUsuarioDeDirectorioActivo = new Tuple<string, List<Modulo>> ( identityname, listaModulos);
                //convert object to json string
                modulosUsuariosString = JsonConvert.SerializeObject(modulosPorUsuarioDeDirectorioActivo);
                //save string in cache
                cache.StringSet(cachekeyname, modulosUsuariosString, TimeSpan.FromMinutes(SettingsHelper.CacheModuleNames));
                return modulosPorUsuarioDeDirectorioActivo;
            }
    

    However I need in the partial view to be able to access the Tuple and then render it.

    model Tuple<string,List<xx.Models.GlobalAdmin.Models.Modulo>;
    
    @foreach (var module in Model.Modules)
    {
        <i class="fa @(@module.ClaseFontAwesome)" title="@module.Nombre"></i>
    }
    

    However I get this error: http://screencast.com/t/lMDg2IYc