How to use tuple in 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 modules for the 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 a partial view in order 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

+4
source share
3 answers

You can access the components of a tuple using the properties Item1 , ... ItemN .

Try the following:

 @foreach (var module in Model.Item2) 
+5
source

Tuple does not have a constructor without parameters, which may be the cause of this problem. Why don't you create a single view model containing a string and a list and pass it to the view? Ref MVC Custom ViewModel and Auto Link

+2
source

you can set @model as a dynamic object and pass it to a Tuple object.

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

All Articles