3

Good day!

I have an application where I need to display and process a form with two different HTML -- one for normal page with site-wide master page and one for an iframe inclusion with different design design (HTML code) and other master page.

Right now I have controller with a couple of actions and views for normal page, it's time to create iframe version. The form fields, validation and processing is identical, so I'd like to make it DRY as possible.

Is it possible to inherit controller (without adding anything new to it) to be able to create new Views?

namespace MyControllers
{
    public class SomeController : BaseController
    {
        [HttpGet]
        public ActionResult ProcessMyForm()
        {
            ...
        }

        [HttpPost]
        public ActionResult ProcessMyForm(FormCollection form)
        {
            ...
        }       
    }
}

Views will be in /Views/MyController

and

namespace MyControllers
{
    public class SomeControllerWithDifferentViews : SomeController
    {
        // nothing here
    }
}

Different views will be in /Views/SomeControllerWithDifferentViews

Does it make sense?

3 Answers 3

5

You could just try passing a query param to your ActionResult handler to know which view to serve up.

[HttpPost]
public ActionResult ProcessMyForm(FormCollection form, bool isIFrame)
{
  ...snip...

  if (isIFrame)
  {
    return View ('MyFormIFrame')
  }
  else
  {
    return View ('MyForm');
  }
}

And if you wanted to map different routes to this handler you could do the following

routes.MapRoute ("MyForm", "/mycontroller/action", new
{
  Controller = "mycontroller",
  Action = "ProcessMyForm",
  isIFrame = false,
});


routes.MapRoute ("MyFormIFrame", "/mycontroller/action-iframe", new
{
  Controller = "mycontroller",
  Action = "ProcessMyForm",
  isIFrame = true,
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the tip, can I put this parameter inside custom URL rewrite rule definition, so I'll, for example have /mycontroller/action-iframe and mycontroller/action as incoming URLs?
1

Is it possible to inherit controller (without adding anything new to it) to be able to create new Views?

Yes, it is.

However, keep in mind that in ASP.NET MVC the views and the controllers are not tied together. You can have two entirely different controllers serve the same view (as long as you pass the correct ViewModel to the view). That means that you don't necessarily need to inherit from the same controller in order to use the view from more than one controller. You can if it makes sense but you don't have to.

Comments

0

Yes, this should work when using the default view engine assuming you have view files present for both the base and derived controller types.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.