Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video teaches users how to migrate an existing Wordpress website to a new domain.
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…

786 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