Does it help to pass a query in MVC 5 to a view without generating migration?

0

Hello, I'm all working on a project in MVC 5 with the Data Entity migrations, generate a table but now to that same project I want to make a query of another existing table, a SELECT and send the records as a table to a view, but not modify anything of the structure of the table, which already has records. Below I leave my code.

Model:

using System;
using System.Data.Entity;

namespace MvcMovie.Models
{
    public class Municipios // clase nombrada igual que la tabla ya existente la cual no fue generada con migraciones
    {
        public int ID { get; set; } // nombrados igual que las columnas
        public string Municipio { get; set; }

        public string Estado { get; set; }

    }

    public class MunicipiosDBContext : DbContext
    {
        public DbSet<Municipios> Municipios { get; set; }
    }
}

Controller:

 private MunicipiosDBContext db = new FormularioDBContext(); 
// GET: /Movies/
public ActionResult Index()
{
    return View(db.Municipios.ToList());
}

View:

@model IEnumerable<Proyecto.Models.Municipios>// aquí tengo problema cuando quiero agregar dos tablas al mismo tiempo,una generada por migraciones que es un archivo, y su clase, y el otro que les muestro aquí, no se la sintaxis para agregar los dos modelos que son archivos separados en models. EN esta view ya muestor unos inputs con valores de la tabla generada por las migraciones, pero tambien quiero mostrar los registros de una tabla generada en sql en esta misma view

@{
    ViewBag.Title = "Details";
}


@foreach (var item in Model) {
    <tr>

         <th>Municipio
               </th>

         <th>Estado
               </th>


        </tr>
        <td>

          <td>id=item.Municipio</td> 
       <td>  id=item.Estado 
        </td>
    </tr>
}
    
asked by Daniel Treviño 01.06.2018 в 05:51
source

1 answer

1

If I do not misunderstand you, what you want to do, (receive two models) is not possible, for that the concept of ViewModel is used, so that it is understood, I will use an example.

I have 2 classes.

A

   public class a

    {
        public string propiedad_1{ get; set; }
        public string propiedad_2{ get; set; }
    }

and B

   public class b

    {
        public string propiedad_3{ get; set; }
        public string propiedad_4{ get; set; }
    }

If I wanted to send both 4 fields to a view, either to present them as a table, or not, what I should do would be a ViewModel , which would be a class like the following

   public class VMayb

    {
        public string propiedad_1{ get; set; }
        public string propiedad_2{ get; set; }
        public string propiedad_3{ get; set; }
        public string propiedad_4{ get; set; }
    }

Then finally, in the controller, I would perform the logic to save the information that I require in my ViewModel ( VMayb )

For example,

public ActionResult Index()
{
    var lista_a = db.a.ToList();
    var lista_completa = new List<VMayb>;
    foreach(var a in lista_a)
    {

     lista_completa.propiedad_1 = a.propiedad_1;
     lista_completa.propiedad_2 = a.propiedad_2;
     lista_completa.propiedad 3 = "otro valor" //Podrías obtener los valores del lugar donde los necesites.
    }
    IEnumerable<VMayb> ListaFinal = lista_completa;
return View(Lista_Final);

}

Then at the hearing, it would be enough to receive it as

@model IEnumerable<Proyecto.ViewModels.VMayb> //o cualquiera sea la ruta donde
//hayas creado la clase.

I hope that is what you were looking for, greetings!

    
answered by 01.06.2018 / 06:07
source