Solved

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

Posted on 2014-12-20
5
228 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

911 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

21 Experts available now in Live!

Get 1:1 Help Now