是否可以點擊一個動作鏈接名稱並讓它像下面的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));
}
任何援助將不勝感激。這甚至有可能嗎?
當然。顯示'CustomerDetailController'的'Index()'方法(儘管我懷疑你真的需要另一種方法(說'Details(int ID)'返回一個顯示細節的視圖) –
你的'@ Html.ActionLink()'正在調用CustomerDetailController的'Index()'方法並將其傳遞給'CustomerNameID',但該方法期望的參數是字符串sortOrder,字符串currentFilter,字符串searchString,int?page'。很難理解這一點,因爲你命名規則混亂,但我懷疑它應該是@ Html.ActionLink(item.CustomerNameName.ToString(),「Detail」,「CustomerName」,new {ID = item.CustomerNameID},...) –