Avatar of Thomas Jones
Thomas Jones
Flag for United States of America

asked on 

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

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.
ASP.NETC#Web Applications

Avatar of undefined
Last Comment
Thomas Jones

8/22/2022 - Mon