I'm having problems with how to make a generic controller. I have Model that the id property can be type of string or int. When i want to Edit a model that id is int, i can't find the action resulting in an error, because i implemented a override method to public abstract ActionResult Edit(string id).
Exists a way to pass a generic parameter(string or int) that my route can be undertand?
public abstract class BaseController<T> : Controller
{
//protected UnitOfWork unitOfWork = new UnitOfWork(System.Web.HttpContext.Current.User.Identity.Name);
protected UnitOfWork unitOfWork = new UnitOfWork("");
public abstract ActionResult List();
public abstract ActionResult Index();
public abstract ActionResult Details(string id);,
public abstract ActionResult CreateByModal();
public abstract ActionResult Create();
[HttpPost]
[ValidateAntiForgeryToken]
public abstract ActionResult Create(T entity);
public abstract ActionResult Edit(string id);
[HttpPost]
[ValidateAntiForgeryToken]
public abstract ActionResult Edit(T entity);
public abstract ActionResult Delete(string id);
protected override void Dispose(bool disposing)
{
//db.Dispose();
base.Dispose(disposing);
}
}
public class ModeloController : BaseController<Modelo>
{
public override ActionResult Edit(string id)
{
//Here i have a int Id im my Model and the error in my route
Modelo modelo = unitOfWork.ModeloRepository.Find(id);
if (modelo == null)
{
return HttpNotFound();
}
return PartialView("_Edit", modelo);
}
}
public class GeneroController : BaseController<Genero>
{
//Here i have a string Id im my Model
public override ActionResult Edit(string id)
{
Genero genero = unitOfWork.GeneroRepository.Find(id);
if (genero == null)
{
return HttpNotFound();
}
return PartialView("_Edit", genero);
}
}
object idas the parameter