MVC Controller question - Calling one Controller from another

  • Thread starter Thread starter Michael Earls
  • Start date Start date
M

Michael Earls

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 said:
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/aspne...lect=2822806#Reusing the presenter in windows>
<http://codebetter.com/blogs/jeremy....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
 
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
 
Michael said:
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/ar...ework-and-data-services-in-action-part-1.aspx>
<http://blogs.microsoft.co.il/blogs/.../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;
}
}
}
}
 
Back
Top