MVC Controller question - Calling one Controller from another

Discussion in 'ASP .Net' started by Michael Earls, Mar 24, 2009.

  1. I'm creating an MVC app based on the Movie DB sample
    (http://www.asp.net/learn/mvc/tutorial-21-cs.aspx). I've created my
    HomeController. Now, I want to add another controller (named
    ProductsController). Products controller has its own subfolder within the
    Views folder withits own Index View.

    What I'd like to do is provide links in my HomeController Index View to the
    Products Controller Index View. I can access the Products Index using the
    url http://localhost:1234/Products but I get an error when I try to iterate
    through my model.

    In other words, when I execute my Product Index View, it's not being
    "backed" by the ProductsController. How do I "link" to my ProductsController
    from my HomeController? Something tells me that this should be very simple,
    but I can't find anything on the web to help out.

    Any ideas?
     
    Michael Earls, Mar 24, 2009
    #1
    1. Advertising

  2. Michael Earls

    MBUnit Guest

    Michael Earls wrote:
    > I'm creating an MVC app based on the Movie DB sample
    > (http://www.asp.net/learn/mvc/tutorial-21-cs.aspx). I've created my
    > HomeController. Now, I want to add another controller (named
    > ProductsController). Products controller has its own subfolder within
    > the Views folder withits own Index View.


    A view and its controller should only be dedicated to its tasks.
    >
    > What I'd like to do is provide links in my HomeController Index View to
    > the Products Controller Index View. I can access the Products Index
    > using the url http://localhost:1234/Products but I get an error when I
    > try to iterate through my model.


    You should not be doing controller to controller access.

    >
    > In other words, when I execute my Product Index View, it's not being
    > "backed" by the ProductsController. How do I "link" to my
    > ProductsController from my HomeController? Something tells me that this
    > should be very simple, but I can't find anything on the web to help out.
    >


    I don't know what you're trying to do here as a Business Layer should be
    involved here and not controller to controller.

    May I suggest this. You should do the examples in the link as MVP
    doesn't rely on a framework and is a spin off MVC with more flexibility.
    And there are lots of examples on how to do MVP.

    What is Model –View- Presenter?

    MVP is a software pattern considered a derivative of the
    Model-view-controller.

    <http://en.wikipedia.org/wiki/Model_View_Presenter>
    <http://msdn.microsoft.com/en-us/magazine/cc188690.aspx>
    <http://mrrask.files.wordpress.com/2008/01/model-view-presenter.pdf>
    <http://www.mvcsharp.org/Reworking_ASPNET_MVC_Store/Default.aspx>
    <http://www.codeproject.com/KB/aspnet/ModelViewPresenter1.aspx?fid=1531640&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2822806#Reusing%20the%20presenter%20in%20windows>
    <http://codebetter.com/blogs/jeremy.miller/archive/2006/02/01/test-driven-development-with-asp-net-and-the-model-view-presenter-pattern.aspx>

    MODEL-VIEW-PRESENTER

    http://www.polymorphicpodcast.com/

    click 'Shows'

    click 'Design Patterns Bootcamp: Model View * Patterns*

    view parts 1-5
     
    MBUnit, Mar 24, 2009
    #2
    1. Advertising

  3. Thanks for the response. It turns out I was passing no data back to the
    View.

    Here's what I had:

    return View();

    Here's what I needed:

    return View(_entities.ProductSet.ToList());

    Thanks for the links on MVP. I'll definitely check them out. It makes me
    glad I posted.

    Michael Earls

    "MBUnit" <> wrote in message
    news:OUA%...
    > Michael Earls wrote:
    >> I'm creating an MVC app based on the Movie DB sample
    >> (http://www.asp.net/learn/mvc/tutorial-21-cs.aspx). I've created my
    >> HomeController. Now, I want to add another controller (named
    >> ProductsController). Products controller has its own subfolder within the
    >> Views folder withits own Index View.

    >
    > A view and its controller should only be dedicated to its tasks.
    >>
    >> What I'd like to do is provide links in my HomeController Index View to
    >> the Products Controller Index View. I can access the Products Index using
    >> the url http://localhost:1234/Products but I get an error when I try to
    >> iterate through my model.

    >
    > You should not be doing controller to controller access.
    >
    >>
    >> In other words, when I execute my Product Index View, it's not being
    >> "backed" by the ProductsController. How do I "link" to my
    >> ProductsController from my HomeController? Something tells me that this
    >> should be very simple, but I can't find anything on the web to help out.
    >>

    >
    > I don't know what you're trying to do here as a Business Layer should be
    > involved here and not controller to controller.
    >
    > May I suggest this. You should do the examples in the link as MVP doesn't
    > rely on a framework and is a spin off MVC with more flexibility. And there
    > are lots of examples on how to do MVP.
    >
    > What is Model –View- Presenter?
    >
    > MVP is a software pattern considered a derivative of the
    > Model-view-controller.
    >
    > <http://en.wikipedia.org/wiki/Model_View_Presenter>
    > <http://msdn.microsoft.com/en-us/magazine/cc188690.aspx>
    > <http://mrrask.files.wordpress.com/2008/01/model-view-presenter.pdf>
    > <http://www.mvcsharp.org/Reworking_ASPNET_MVC_Store/Default.aspx>
    > <http://www.codeproject.com/KB/aspnet/ModelViewPresenter1.aspx?fid=1531640&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2822806#Reusing%20the%20presenter%20in%20windows>
    > <http://codebetter.com/blogs/jeremy.miller/archive/2006/02/01/test-driven-development-with-asp-net-and-the-model-view-presenter-pattern.aspx>
    >
    > MODEL-VIEW-PRESENTER
    >
    > http://www.polymorphicpodcast.com/
    >
    > click 'Shows'
    >
    > click 'Design Patterns Bootcamp: Model View * Patterns*
    >
    > view parts 1-5
    >
    >
    >
     
    Michael Earls, Mar 24, 2009
    #3
  4. Michael Earls

    MBUnit Guest

    Michael Earls wrote:
    > Thanks for the response. It turns out I was passing no data back to the
    > View.
    >
    > Here's what I had:
    >
    > return View();
    >
    > Here's what I needed:
    >
    > return View(_entities.ProductSet.ToList());
    >
    > Thanks for the links on MVP. I'll definitely check them out. It makes me
    > glad I posted.
    >
    > Michael Earls
    >


    If you want to do some more, you should check this out.

    <http://geekswithblogs.net/frankw/archive/2008/03/08/ado.net-entity-framework-and-data-services-in-action-part-1.aspx>
    <http://blogs.microsoft.co.il/blogs/bursteg/archive/2008/05/12/new-assemblies-net-framework-3-5-sp1.aspx>
    <http://blogs.msdn.com/astoriateam/>
    http://marlongrech.wordpress.com/category/linq/


    I'll give you a little help to put it together, as I am implementing in
    solution I am using at work, kind of like an internal "Link Provider"
    for searching data in the Model with Link.

    You can get rid of the Default.aspx as it's not needed when it becomes a
    Dataservice.

    using System;
    using System.Data.Services;

    namespace NorthwindModel
    {
    public class Northwind : DataService<NorthwindEntities>
    {
    // This method is called only once to initialize service-wide
    policies.
    public static void InitializeService(IDataServiceConfiguration
    config)
    {
    // TODO: set rules to indicate which entity sets and
    service operations are visible, updatable, etc.
    // Examples:
    // config.SetEntitySetAccessRule("MyEntityset",
    EntitySetRights.AllRead);
    try
    {
    config.SetEntitySetAccessRule("*",
    EntitySetRights.AllRead);
    config.SetServiceOperationAccessRule("*",
    ServiceOperationRights.AllRead);
    }
    catch (Exception ex)
    {
    string msg = ex.Message;
    }
    }
    }
    }

    ---------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Data.Services.Client;

    namespace TestNorthwind
    {
    public class Shippers
    {
    public long ShipperID { get; set; }
    public string CompanyName { get; set; }
    public string Phone { get; set; }

    public static IEnumerable<Shippers> GetAllShippers()
    {
    var siteuri = new
    Uri("http://localhost/NorthwindDataService1/Northwind.svc/");
    var context = new DataServiceContext(siteuri);

    return from s in context.CreateQuery<Shippers>("Shippers")
    where s.CompanyName.Contains("Pac")
    select s;
    }
    }
    }


    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    using System;

    namespace TestNorthwind
    {
    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    var shps = Shippers.GetAllShippers();

    foreach (var shp in shps)
    {
    string name = shp.CompanyName;
    }
    }
    }
    }
     
    MBUnit, Mar 24, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet

    MVC: Controller for Swing

    VisionSet, Nov 5, 2003, in forum: Java
    Replies:
    1
    Views:
    480
    Harald Hein
    Nov 5, 2003
  2. rmn190
    Replies:
    2
    Views:
    2,476
    Arne Vajhøj
    Jan 10, 2008
  3. Paulo
    Replies:
    3
    Views:
    511
    Peter Bromberg [C# MVP]
    Dec 4, 2008
  4. Crazy Cat
    Replies:
    1
    Views:
    725
    schepp
    Sep 3, 2009
  5. Ralph Shnelvar

    MVC .... Many views and one controller

    Ralph Shnelvar, Jan 18, 2010, in forum: Ruby
    Replies:
    6
    Views:
    187
    Walton Hoops
    Jan 18, 2010
Loading...

Share This Page