Solved

How to pass an ID from an actionlink to a different controller - MVC 5

Posted on 2014-12-20
5
222 Views
Last Modified: 2014-12-26
How do I perform the following sql query with an ActionLink.

SQL Query:
SELECT        dbo.CustomerName.CustomerNameName, dbo.CustomerDetail.CustomerNameID
FROM            dbo.CustomerDetail INNER JOIN
                         dbo.CustomerName ON dbo.CustomerDetail.CustomerNameID = dbo.CustomerName.CustomerNameID
WHERE        (dbo.CustomerDetail.CustomerNameID = 1)

Open in new window



Model: CustomerName
   public partial class CustomerName
    {
        public CustomerName()
        {
            this.CustomerDetails = new HashSet<CustomerDetail>();
            this.CustomerEquipments = new HashSet<CustomerEquipment>();
            this.CustomerHealthChecks = new HashSet<CustomerHealthCheck>();
        }
    
        public int CustomerNameID { get; set; }
        public Nullable<int> CustomerHealthCheckID { get; set; }
        public Nullable<int> CustomerEquipmentID { get; set; }
        public int MasterLicNum { get; set; }
        public string CustomerNameName { get; set; }
        public Nullable<int> Active { get; set; }

        public virtual CustomerDetail CustomerDetail { get; set; }
    
        public virtual ICollection<CustomerDetail> CustomerDetails { get; set; }
        public virtual ICollection<CustomerEquipment> CustomerEquipments { get; set; }
        public virtual ICollection<CustomerHealthCheck> CustomerHealthChecks { get; set; }
    }
}

Open in new window


Model: CustomerDetail
    public partial class CustomerDetail
    {
        public CustomerDetail()
        {
            this.CustomerNotes = new HashSet<CustomerNote>();
        }
    
        public int CustomerDetailID { get; set; }
        public int CustomerNameID { get; set; }
        public System.DateTime DateUpdated { get; set; }
        public System.DateTime DateCreated { get; set; }
        public int CustomerPriorityID { get; set; }
        public int CustomerStatusHealthID { get; set; }
        public Nullable<int> EquipmentID { get; set; }
        public Nullable<int> CustomerHealthCheckID { get; set; }
        public int LREngineerID { get; set; }
        public int CustomerEngineerID { get; set; }
        public string Description { get; set; }
        public byte[] RowVersion { get; set; }
    
        public virtual CustomerEngineer CustomerEngineer { get; set; }
        public virtual CustomerName CustomerName { get; set; }
        public virtual CustomerPriority CustomerPriority { get; set; }
        public virtual CustomerStatusHealth CustomerStatusHealth { get; set; }
        public virtual LREngineer LREngineer { get; set; }
        public virtual ICollection<CustomerNote> CustomerNotes { get; set; }
    }
}

Open in new window


Controller:
return View(CustomerName.ToPagedList(pageNumber, pageSize));

Open in new window


View:
@Html.ActionLink(item.CustomerNameName.ToString(), "Index", "CustomerDetail", new {ID = item.CustomerNameID }, null)

Open in new window


Route:
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapMvcAttributeRoutes();
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "CustomerName", action = "Index", id = UrlParameter.Optional }

Open in new window


The return result I get is the index of CustomerDetail but "All" the records.  I just want the records associated with the CustomerNameName ToString.  If anyone has any ideas please let me know.
0
Comment
Question by:Thomas Jones
  • 3
5 Comments
 
LVL 25

Expert Comment

by:apeter
ID: 40511906
All code looks fine. Please provide more information on below.

1. Are you able to pass ID from view to controller ?
2. Did you check in SQL Management studio, for the passed id value the returned rows returned looks okie.

Find which area/layer is not working as expected ?
0
 

Author Comment

by:Thomas Jones
ID: 40512096
By looking at the out put it seems to be passing the CustomerNameID from the CustomerName/Index to the CustomerDetail/Index.  I must be missing some code in the CustomerDetail controller.  Thoughts?

CustomerDetailController
0
 

Accepted Solution

by:
Thomas Jones earned 0 total points
ID: 40512388
Thanks for the help apeter but I finally, I mean FINALLY, figured it out.  For some reason the ID passed from the CustomerName/Index was not being filtered in the CustomerDetails/Index.  I added the code below and it worked!!!!  Maybe I just worded the question wrong.

        // GET: CustomerDetail
        public async Task<ActionResult> Index(int CustomerNameID)
        {

            IQueryable<CustomerDetail> CustomerDetail = db.CustomerDetails
               .Where(c => c.CustomerNameID == CustomerNameID)
               .OrderBy(d => d.CustomerNameID)
               .Include(d => d.CustomerName);
     

            return View(await CustomerDetail.ToListAsync());
        }

Open in new window

0
 

Author Closing Comment

by:Thomas Jones
ID: 40518279
I was unable to get the correct answer from the experts or other websites.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now