0
I have the next situation, in my Models folder, I have:
public class HomeViewModel
{
public HomeViewModel()
{
// apenas para garantir que NUNCA seja nulo! Facilica código na view
PreviewImages = new List<Generico.Dominio.TB_IMAGEN_PERFIL>();
InitialPreviewConfigImages = new List<Generico.Dominio.TB_IMAGEN_PERFIL>();
User = new Generico.Dominio.TB_USUARIO();
}
//imagem do perfil do usuário
public List<Generico.Dominio.TB_IMAGEN_PERFIL> PreviewImages { get; set; }
public List<Generico.Dominio.TB_IMAGEN_PERFIL> InitialPreviewConfigImages { get; set; }
public Generico.Dominio.TB_USUARIO User { get; set; }
}
I want to bring the registration option along with the image gallery option: On my view today I have a normal record:
@model Generic.Dominio.TB_USUARIO
//view registration data
//photo gallery data in View
In the controller I have:
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
public ActionResult ConsultaCadastroUsuarioCompleto(int id)
{
try
{
var tbuscar = new UsuarioAplicacao();
TB_USUARIO tbtabela = tbuscar.ListarPoId(id);
var model = new HomeViewModel
{
User = tbtabela,
PreviewImages = new List<Generico.Dominio.TB_IMAGEN_PERFIL> {
new Generico.Dominio.TB_IMAGEN_PERFIL { Id = 1, Url = Url.Content("~/Content/img/galeriaimagens/sl1.jpg") },
new Generico.Dominio.TB_IMAGEN_PERFIL { Id = 2, Url = Url.Content("~/Content/img/galeriaimagens/sl2.jpg") },
new Generico.Dominio.TB_IMAGEN_PERFIL { Id = 3, Url = Url.Content("~/Content/img/galeriaimagens/sl3.jpg") },
new Generico.Dominio.TB_IMAGEN_PERFIL { Id = 4, Url = Url.Content("~/Content/img/galeriaimagens/sl4.jpg") },
},
// size será preenchido depois (mas se vier do banco de dados, PREENCHA aqui para evitar perda de performance
InitialPreviewConfigImages = new List<Generico.Dominio.TB_IMAGEN_PERFIL> {
new Generico.Dominio.TB_IMAGEN_PERFIL { Id = 1, Url = Url.Content("~/Content/img/galeriaimagens/sl1.jpg"), Name = "Food-1.jpg" },
new Generico.Dominio.TB_IMAGEN_PERFIL { Id = 2, Url = Url.Content("~/Content/img/galeriaimagens/sl2.jpg"), Name = "Food-2.jpg" },
new Generico.Dominio.TB_IMAGEN_PERFIL { Id = 3, Url = Url.Content("~/Content/img/galeriaimagens/sl3.jpg"), Name = "Food-3.jpg" },
new Generico.Dominio.TB_IMAGEN_PERFIL { Id = 4, Url = Url.Content("~/Content/img/galeriaimagens/sl4.jpg"), Name = "Food-4.jpg" },
}
};
FindFileSizes(model.InitialPreviewConfigImages);
return View(model);
}
catch (Exception)
{
TempData["Erro"] = "Erro ao Alterar Registro.";
return RedirectToAction("Index", "CadastroCompletoUsuario");
}
}
private void FindFileSizes(List<TB_IMAGEN_PERFIL> imgs)
{
foreach (var img in imgs)
{
// é preciso converter o caminho relativo da URL em um caminho físico no servidor
var serverPath = Server.MapPath(img.Url);
if (System.IO.File.Exists(serverPath))
{
img.Size = new System.IO.FileInfo(serverPath).Length;
}
}
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
In Usersapplication(); has a method that makes a query bringing the data of 1 user.
in View I have
@model Design.WebSite.Models.Homeviewmodel
But I can’t access the table, how would that look?
Put the list code by Id and also the code of the
tbuscar.ListarTodos();
to be able to see how the data is coming from the database, because it will be necessary to manipulate them to return in the format that your views expect the data.– Loudenvier
It would also modify your viewmodel to not have a list of user data, but only the data of a user: instead of
public List<Generico.Dominio.TB_USUARIO> TabelaUsuario { get; set; }
I would dopublic Generico.Dominio.TB_USUARIO TabelaUsuario { get; set; }
... actually I would create another User class only with the data pertinent to the view... but this is not so important in your case as you are not sending it via JSON, since you are only dealing with it on the server...– Loudenvier