Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 327
Luis Valencia Avatar asked Jan 08 '23 20:01

Luis Valencia


2 Answers

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

Try this:

@foreach (var module in Model.Item2)
like image 97
Hamlet Hakobyan Avatar answered Jan 10 '23 10:01

Hamlet Hakobyan


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

like image 32
KnightFox Avatar answered Jan 10 '23 10:01

KnightFox