How can I inherit an ASP.NET MVC controller and change only the view?

I have a controller that inherits from the base controller, and I wonder how I can use all the logic from the base controller, but return a different view than the base controller uses.

The base controller populates the model object and passes that model object to its view, but I'm not sure how I can access this model object in the child controller so that I can pass it to the view of the child controller.

+5
source share
5 answers

Based on the feedback received on this topic, I implemented a solution similar to the proposal by Anthony Koch.

, GetIndex, .

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);
    }
}
+1

. ViewResult, , , . . , MVC v1, View (object) ViewData.Model , ViewResult.

Controller.cs: 440

protected internal ViewResult View(object model) {
    return View(null /* viewName */, null /* masterName */, model);
}

Controller.cs: 456

protected internal virtual ViewResult View(string viewName, string masterName, object model) {
    if (model != null) {
        ViewData.Model = model;
    }

    return new ViewResult {
        ViewName = viewName,
        MasterName = masterName,
        ViewData = ViewData,
        TempData = TempData
    };
}

, , , View (string).

namespace BaseControllers
{
    public class CoolController
    {
        public virtual ViewResult Get() 
        {
            var awesomeModel = new object();
            return View(awesomeModel);
        }
    }
}

public class CoolController : BaseControllers.CoolController
{
    public override ViewResult Get() 
    {
        var ignoredResult = base.Get();
        // ViewData.Model now refers to awesomeModel
        return View("NotGet");
    }
}

, , ViewResult, . :

public class CoolController : BaseControllers.CoolController
{
    public override ViewResult Get() 
    {
        var baseResult = base.Get();
        baseResult.ViewName = "NotGet";
        return baseResult;
    }
}

ActionResult, ViewResult View.

+7

:

:

public abstract class BaseTableController<T,TU> : BaseController where TU : IGenericService<T>,IModelWrapperService
{
    protected readonly TU _service;

    public BaseTableController(TU service)
    {
        _service = service;
        _service.ModelWrapper = new ControllerModelStateWrapper(ModelState);
    }


    public ActionResult Index()
    {
        return View(_service.List());
    }

:

public class SeverityController : BaseTableController<Severity, ISeverityService>
{
    public SeverityController(ISeverityService service)
        : base(service)
    {
    }

   //NO CODE INSIDE
}

SeverityController.Index() Views/Severity/Index.aspx. . . , .

+3

- viewName.

, .

- ?

public class SalesController : Controller
{
    public virtual ActionResult Index(string viewName)
    {
        ViewData["test"] = "Set in base.";

        TestModel model = new TestModel();
        model.Text = "123";

        return String.IsNullOrEmpty(viewName) ? View(model) : View(viewName, model);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public virtual ActionResult Index(TestModel data, FormCollection form, string viewName)
    {
        TryUpdateModel(data, form.ToValueProvider());
        return String.IsNullOrEmpty(viewName) ? View(data) : View(viewName, data);
    }
}

public class SalesController : MyApp.Controllers.BaseControllers.SalesController
{
    public override ActionResult Index(string viewName)
    {
        return base.Index("ClientIndex");
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public override ActionResult Index(TestModel data, FormCollection form, string viewName)
    {
        return base.Index(data, form, "ClientIndex");
    }
}
0
public class BaseController : Controller {
protected BaseController() {}

public ActionResult Index()
{
    return GetIndex();
}

public abstract ActionResult GetIndex();  }

public class MyController : BaseController {
public MyController() {}

public override GetIndex()
{   
    return RedirectToAction("Cakes","Pies");
} 

}

, .

0

All Articles