Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 116 characters in body
Source Link
Bret Walker
  • 1.8k
  • 5
  • 22
  • 41

Based on the feedback given on this thread, I've implemented a solution like the one proposed by Antony Koch.

Instead of using an abstract method, I used a concrete, virtual GetIndex method so that I could put logic in it for the base controller.

public class SalesController : Controller { // Index view method and model public virtual ActionResult GetIndex() { return View("Index", IndexModel); } protected TestModel IndexModel { get; set; } public virtual ActionResult Index() { ViewData["test"] = "Set in base."; IndexModel = new TestModel(); IndexModel.Text = "123"; return GetIndex(); } [AcceptVerbs(HttpVerbs.Post)] public virtual ActionResult Index(TestModel data, FormCollection form) { TryUpdateModel(data, form.ToValueProvider()); IndexModel = data; return GetIndex(); } } // This class will need to be in a different namespace or named differently than the // parent controller public class SalesController : MyApp.Controllers.BaseControllers.SalesController { // Index view method and model public override ActionResult GetIndex() { return View("ClientIndex", IndexModel); } public override ActionResult Index() { return base.Index(); } [AcceptVerbs(HttpVerbs.Post)] public override ActionResult Index(TestModel data, FormCollection form) { return base.Index(data, form); } } 

Based on the feedback given on this thread, I've implemented a solution like the one proposed by Antony Koch.

Instead of using an abstract method, I used a concrete, virtual GetIndex method so that I could put logic in it for the base controller.

public class SalesController : Controller { // Index view method and model public virtual ActionResult GetIndex() { return View("Index", IndexModel); } protected TestModel IndexModel { get; set; } public virtual ActionResult Index() { ViewData["test"] = "Set in base."; IndexModel = new TestModel(); IndexModel.Text = "123"; return GetIndex(); } [AcceptVerbs(HttpVerbs.Post)] public virtual ActionResult Index(TestModel data, FormCollection form) { TryUpdateModel(data, form.ToValueProvider()); IndexModel = data; return GetIndex(); } } public class SalesController : MyApp.Controllers.BaseControllers.SalesController { // Index view method and model public override ActionResult GetIndex() { return View("ClientIndex", IndexModel); } public override ActionResult Index() { return base.Index(); } [AcceptVerbs(HttpVerbs.Post)] public override ActionResult Index(TestModel data, FormCollection form) { return base.Index(data, form); } } 

Based on the feedback given on this thread, I've implemented a solution like the one proposed by Antony Koch.

Instead of using an abstract method, I used a concrete, virtual GetIndex method so that I could put logic in it for the base controller.

public class SalesController : Controller { // Index view method and model public virtual ActionResult GetIndex() { return View("Index", IndexModel); } protected TestModel IndexModel { get; set; } public virtual ActionResult Index() { ViewData["test"] = "Set in base."; IndexModel = new TestModel(); IndexModel.Text = "123"; return GetIndex(); } [AcceptVerbs(HttpVerbs.Post)] public virtual ActionResult Index(TestModel data, FormCollection form) { TryUpdateModel(data, form.ToValueProvider()); IndexModel = data; return GetIndex(); } } // This class will need to be in a different namespace or named differently than the // parent controller public class SalesController : MyApp.Controllers.BaseControllers.SalesController { // Index view method and model public override ActionResult GetIndex() { return View("ClientIndex", IndexModel); } public override ActionResult Index() { return base.Index(); } [AcceptVerbs(HttpVerbs.Post)] public override ActionResult Index(TestModel data, FormCollection form) { return base.Index(data, form); } } 
Source Link
Bret Walker
  • 1.8k
  • 5
  • 22
  • 41

Based on the feedback given on this thread, I've implemented a solution like the one proposed by Antony Koch.

Instead of using an abstract method, I used a concrete, virtual GetIndex method so that I could put logic in it for the base controller.

public class SalesController : Controller { // Index view method and model public virtual ActionResult GetIndex() { return View("Index", IndexModel); } protected TestModel IndexModel { get; set; } public virtual ActionResult Index() { ViewData["test"] = "Set in base."; IndexModel = new TestModel(); IndexModel.Text = "123"; return GetIndex(); } [AcceptVerbs(HttpVerbs.Post)] public virtual ActionResult Index(TestModel data, FormCollection form) { TryUpdateModel(data, form.ToValueProvider()); IndexModel = data; return GetIndex(); } } public class SalesController : MyApp.Controllers.BaseControllers.SalesController { // Index view method and model public override ActionResult GetIndex() { return View("ClientIndex", IndexModel); } public override ActionResult Index() { return base.Index(); } [AcceptVerbs(HttpVerbs.Post)] public override ActionResult Index(TestModel data, FormCollection form) { return base.Index(data, form); } }