Pass generic model to controller

Asked

Viewed 571 times

2

My project follows the following model:

inserir a descrição da imagem aqui

In my Controller I have the following: inserir a descrição da imagem aqui

I noticed you might be using this ActionResult dynamically. My view is typical.

I wonder if there’s a way to pass the kind of mine view pro model or some way to dynamically receive the model in my controller. Being in the example, man model "Groupomogeneus".

In short, my intention is to pass the name of the model and somehow create something that returns the model filtered by name in my ActionResult, as in the case of a System.Object

"Example" of what I want:

        var Nome = "GrupoHomogeneoEF";
        var bdModel = new (Nome)(contexto);
  • 1

    What is the purpose of this?

  • @Romaniomorrisonmendez Reuse the same Actionresult for more than one model

  • Tipado can’t do it. The Models have a common ancestor?

  • Yes yes. Just for the record, I can change my architecture to fit in some way to be able to do what I want

2 answers

4

I still don’t understand what you want to do, but the View accepted by default the following:

@model dynamic

I mean, you can always pass anything.

Of course this has consequences. You need to keep checking whether the property of Model exists:

@if (Model.GetType().GetProperty("propriedade") != null) { ... }

I would also make a Controller generic:

public abstract class Controller<T> : System.Web.Mvc.Controller
    where T: class, new() { ... }

And the Action GrupoHomogeneo:

public virtual ActionResult GrupoHomogeneo(T objeto, FormCollection collection) { ... }

Or even, you can define a common ancestor:

public abstract class ModelAncestralComum { ... }

And restrict the Controller generic:

public abstract class Controller<T> : System.Web.Mvc.Controller
    where T: ModelAncestralComum, new() { ... }
  • I believe that generic Controller would help me. I tried here but get errors, could you put my structure in your example? Applying public abstract class WebController<T> : Controller&#xA; where T: class, new() { Aplicacao.Core.RepositorioEF.Base.Repositorio<T>} receive message "Invalid token '}' in class, struct, or interface Member declaration"

  • Yes, edit your question by placing the code as text that I change for you.

2

Well, from what I understand you want to make your Generic controller and have a Generic action, but the view will continue to be specialized.

Below is a very direct suggestion to your point, but would N other improvements that could be made, you could read this article for more information.

public class GenericController<TObjeto, TRepositorio> : Controller
    where TObjeto : ObjetoBase, new()
    where TRepositorio : RepositorioEFBase, new()
{
    [HttpPost]
    public virtual ActionResult AcaoGenerica(TObjeto objeto, FormCollection collection)
    {
        var repositorio = new RepositorioEFBase(contexto);
        ...
        return RedirectToAction(collection["ReturnView"], objeto);
    }
}

public class GrupoHomogeneoController : GenericController<GrupoHomogeneo, GrupoHomogeneoRepositorioEF>
{
}

public abstract class ObjetoBase
{
    public int ID {get; set;}
}

public class GrupoHomogeneo : ObjetoBase
{
    public string Campo1 {get; set;}
}

public abstract class RepositorioEFBase<TObjeto>
    where TObjeto : ObjetoBase, new()
{
    private DbContext _contexto;
    public RepositorioEFBase(DbContext contexto)
    {
            _contexto = contexto;
    }

    //utilize esse DbSet para os seus metodos genericos
    internal IDbSet<TObjeto> DbSet { get { return _contexto.Set<TObjeto>(); } }

    //metodos genericos do repositorio
}

public class GrupoHomogeneoRepositorioEF : RepositorioEFBase<GrupoHomogeneo>
{

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.