Access a model from the controller of a ViewModel

2

I am using ASP.NET MVC4 and I find it difficult to bring data stored in the tables in the database.

I understand that there are some clauses ToList() that could list the data, but currently the methods I use always return me null .

The code that I am currently implementing is:

Controller (ViewModel):

    public ActionResult EditForm()
   {
        FormularioFlota2 FF2 = new FormularioFlota2();
        FF2.FormularioFlota_Persona = GetPersona();
        return View(FF2);
    }

    public DbSet<Persona> DbPersonas { get; set; }

    public List<Persona> GetPersona()
    {
        List<Persona> listaDePersonasPrueba = new List<Persona>();
        listaDePersonasPrueba = DbPersonas.ToList();


        return listaDePersonasPrueba;
    }

View (ViewModel):

 @if (Model.FormularioFlota_Persona != null && Model.FormularioFlota_Persona.Count > 0)
            {
                foreach (var item in Model.FormularioFlota_Persona)
                {
            <tr>
                <td>
                   @item.Id
                </td>
                <td>
                    @item.NumeroLegajo
                </td>
                <td>
                    @item.Nombre
                </td>
                <td>
                    @item.Apellido
                </td>
                <td>
                    @item.Calle
                </td>
                <td>
                    @item.Localidad
                </td>
                <td>
                    @item.Provincia
                </td>
                <td>
                    @item.Telefono
                </td>
                <td>
                    @item.Email
                </td>
                <td>
                </td>
                <td>
                    @item.Documento
                </td>
                <td>
                    @item.Cuil
                </td>
                <td>
                    @item.EstadoCivil
                </td>
                <td>
                    @item.Cargo
                </td>
                <td>
                    @item.SectorId
                </td>
                <td>
                    @item.GrupoBeneficiarioId
                </td>
            </tr>
                    }
                }
  

Note: for some reason the TD Y TR tags do not detect me as   text, that's why in the View You see everything stuck, but basically I walk   vector FormularioFlotaPersona and assign the fields, that's when   I failed, the compiler tells me that he receives " null "

Does anyone know a method that can be applied in a controller (from viewmodel) to bring all the data stored in a table? I have all the tables defined in different models and working correctly. Ideally, avoid using Queryable commands.

    
asked by seojedaperez 02.05.2017 в 17:42
source

1 answer

1

In your controller you must have your entities:

private BaseDeDatosEntities db = new BaseDeDatosEntities(); 
//o como lo hayas definido en el Context

A recommendation in the following ActionResult:

public ActionResult EditForm()
{
    //Cada vez que vayas a pasar un model:
    //utiliza la misma palabra para que te sea más fácil de identificarlos.
    FormularioFlota2 model = new FormularioFlota2();
    model.FormularioFlota_Persona = GetPersona();
    return View(model);
}  

Here when you bring the entities, be sure to first call the entities and within them is the table with which you want to work, because you skip that detail, it's because you get everything null.

public List<Persona> GetPersona()
{
    List<Persona> listaDePersonasPrueba = new List<Persona>();
    listaDePersonasPrueba = db.DbPersonas.ToList();
    return listaDePersonasPrueba;
}

Additional details:

  • In the ActionResults where you are going to modify information, it is recommended to have the form of the table / entity that you are going to modify.
  • If you are going to list and then modify, that list can go smoothly in a different ActionResult.
  • Remember to use DataAnnotations from EntityFramework + jQuery unobtrusive to make your life easier when performing input validations.
  • Take ASP.net MVC classes at link - link
  • answered by 02.05.2017 / 18:40
    source