2014-12-21 55 views
2

是否可以點擊一個動作鏈接名稱並讓它像下面的sql語句一樣檢索關聯的記錄?客戶名稱html鏈接 - >轉到客戶詳細信息視圖的索引,只檢索與客戶名稱(CustomerNameID)關聯的記錄。如何使用ActionLink MVC 5執行以下SQL查詢?

SELECT dbo.CustomerName.CustomerNameName, dbo.CustomerDetail.CustomerNameID 
FROM dbo.CustomerDetail INNER JOIN 
    dbo.CustomerName ON dbo.CustomerDetail.CustomerNameID = dbo.Cust 

型號:客戶名稱

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; } 
    } 
} 

型號:爲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; } 
    } 
} 

控制器:

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

查看:

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

路線:

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

我重建控制器把它清理乾淨。這是目前標準的開箱即用控制器。 CustomerNameController指數():

// GET: CustomerName 
public async Task<ActionResult> Index(int? CustomerNameID = null) 
{ 
    return View(await db.CustomerNames.ToListAsync()); 
} 

// GET: CustomerName/Details/5 
public async Task<ActionResult> Details(int? id) 
{ 
    if (id == null) 
    { 
     return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
    } 
    CustomerName customerName = await db.CustomerNames.FindAsync(id); 
    if (customerName == null) 
    { 
     return HttpNotFound(); 
    } 
    return View(customerName); 
} 

CustomerDetailController指數():

public async Task<ActionResult> Index(string sortOrder, string currentFilter, string searchString, int? page) 
     { 
      var customerDetails = db.CustomerDetails.Include(c => c.CustomerEngineer).Include(c => c.CustomerName).Include(c => c.CustomerPriority).Include(c => c.CustomerStatusHealth).Include(c => c.LREngineer); 
      ViewBag.CurrentSort = sortOrder; 
      ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; 
      ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date"; 

      if (searchString != null) 
      { 
       page = 1; 
      } 
      else 
      { 
       searchString = currentFilter; 
      } 

      ViewBag.CurrentFilter = searchString; 
      var CustomerDetails = from s in db.CustomerDetails 
          select s; 
      if (!String.IsNullOrEmpty(searchString)) 
      { 
       CustomerDetails = CustomerDetails.Where(s => s.Description.Contains(searchString)); 
      } 
      switch (sortOrder) 
      { 
       case "name_desc": 
        CustomerDetails = CustomerDetails.OrderByDescending(s => s.CustomerNameID); 
        break; 
       case "Date": 
        CustomerDetails = CustomerDetails.OrderBy(s => s.DateUpdated); 
        break; 
       case "date_desc": 
        CustomerDetails = CustomerDetails.OrderByDescending(s => s.DateUpdated); 
        break; 
       default: // Name ascending 
        CustomerDetails = CustomerDetails.OrderBy(s => s.CustomerNameID); 
        break; 
      } 

      int pageSize = 15; 
      int pageNumber = (page ?? 1); 

      //return View(await customerDetails.ToListAsync()); 
      return View(CustomerDetails.ToPagedList(pageNumber, pageSize)); 
     } 

任何援助將不勝感激。這甚至有可能嗎?

+0

當然。顯示'CustomerDetailController'的'Index()'方法(儘管我懷疑你真的需要另一種方法(說'Details(int ID)'返回一個顯示細節的視圖) –

+0

你的'@ Html.ActionLink()'正在調用CustomerDetailController的'Index()'方法並將其傳遞給'CustomerNameID',但該方法期望的參數是字符串sortOrder,字符串currentFilter,字符串searchString,int?page'。很難理解這一點,因爲你命名規則混亂,但我懷疑它應該是@ Html.ActionLink(item.CustomerNameName.ToString(),「Detail」,「CustomerName」,new {ID = item.CustomerNameID},...) –

回答

0

答案是很簡單但我沒能抓住它。在多次運行程序後,我注意到客戶名稱/索引ID已被傳遞,但CustomerDetail /索引無法將其提取並篩選結果。我添加了下面的代碼,它終於奏效了。感謝所有的幫助。

 // 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()); 
    } 
-1

編輯

SELECT dbo.CustomerName.CustomerNameName, dbo.CustomerDetail.CustomerNameID 
FROM dbo.CustomerDetail INNER JOIN 
    dbo.CustomerName ON dbo.CustomerDetail.CustomerNameID = dbo.Cust 

在LINQ:

var query = from detail in CustomerDetails 
      //from name in CustomerNames.Where(r=>r.CustomerDetail == detail) 
      from name in CustomerNames.Where(r=>r.CustomerNameID == detail.CustomerNameID) 
      select new {CustomerName = name.CustomerName, CustomerNameId = detail.CustomerNameId} 

OFF

如果你想在同一個頁面上顯示的詳細信息,修改Index行動。創建一個包含客戶列表和詳細信息對象的模型。

檢查null的輸入參數,如果是這樣,只返回客戶列表。如果參數不爲空,則返回包含客戶詳細信息的客戶列表。

public ViewResult Index(int? customerId = null) 
{ 
    if(cusomerId == null) 
    { 
     return new IndexModel {Customers = customersList}; 
    } 
    else 
    { 
     return new IndexModel {Customers = customersList, Details = getDetailsById(customerId)}; 
    } 
} 

型號示例:

public class IndexModel 
{ 
    public List<Customer> Customers {get;set;} 
    public CustomerDetails Details {get;set;} 
} 

的ActionLink:

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