[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2014-12-20
5
Medium Priority
?
386 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

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…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

649 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