是否可以单击动作链接名称,并使其像下面的sql语句一样检索关联的记录? CustomerName html链接->转到客户详细信息视图的索引,仅检索与客户名称(CustomerNameID)关联的记录。
SELECT dbo.CustomerName.CustomerNameName, dbo.CustomerDetail.CustomerNameID
FROM dbo.CustomerDetail INNER JOIN
dbo.CustomerName ON dbo.CustomerDetail.CustomerNameID = dbo.Cust
型号: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; }
}
}
型号:客户详细
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 Index():
// 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 Index():
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));
}
任何援助将不胜感激。这有可能吗?
最佳答案
答案很简单,但我未能掌握。在无数次运行该程序之后,我注意到传递了CustomerName / Index ID,但是CustomerDetail / Index未能选择它并筛选结果。我添加了以下代码,终于成功了。谢谢大家的帮助。
// 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());
}
关于c# - 如何使用ActionLink MVC 5执行以下sql查询?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27587281/