C# 如何使用ActionLink MVC 5执行以下sql查询?

C# 如何使用ActionLink MVC 5执行以下sql查询?,c#,asp.net-mvc,razor-2,C#,Asp.net Mvc,Razor 2,是否可以像下面的sql语句一样单击actionlink名称并让它检索相关记录?CustomerName html链接-->转到客户详细信息视图的索引,仅检索与客户名称(CustomerName ID)关联的记录 型号:CustomerName public partial class CustomerName { public CustomerName() { this.CustomerDetails = new HashSet&

是否可以像下面的sql语句一样单击actionlink名称并让它检索相关记录?CustomerName html链接-->转到客户详细信息视图的索引,仅检索与客户名称(CustomerName ID)关联的记录

型号: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; }
    }
}
视图:

路线:

 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索引():

//获取:CustomerName
公共异步任务索引(int?CustomerNameID=null)
{
返回视图(wait db.CustomerNames.toListSync());
}
//获取:CustomerName/Details/5
公共异步任务详细信息(int?id)
{
if(id==null)
{
返回新的HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
CustomerName CustomerName=await db.CustomerNames.FindAsync(id);
if(customerName==null)
{
返回HttpNotFound();
}
返回视图(客户名称);
}
CustomerDetailController索引():

public异步任务索引(string sortOrder、string currentFilter、string searchString、int?页)
{
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=排序器;
ViewBag.NameSortParm=String.IsNullOrEmpty(排序器)?“名称描述”:“;
ViewBag.DateSortParam=sortOrder==“日期”?“日期描述”:“日期”;
if(searchString!=null)
{
page=1;
}
其他的
{
searchString=currentFilter;
}
ViewBag.CurrentFilter=搜索字符串;
var CustomerDetails=来自数据库中的s.CustomerDetails
选择s;
如果(!String.IsNullOrEmpty(searchString))
{
CustomerDetails=CustomerDetails.Where(s=>s.Description.Contains(searchString));
}
开关(分拣机)
{
案例“名称描述”:
CustomerDetails=CustomerDetails.OrderByDescending(s=>s.CustomerNameID);
打破
案件“日期”:
CustomerDetails=CustomerDetails.OrderBy(s=>s.DateUpdated);
打破
案例“日期描述”:
CustomerDetails=CustomerDetails.OrderByDescending(s=>s.DateUpdated);
打破
默认值://Name升序
CustomerDetails=CustomerDetails.OrderBy(s=>s.CustomerNameID);
打破
}
int pageSize=15;
整数页码=(第1页);
//返回视图(等待customerDetails.toListSync());
返回视图(CustomerDetails.ToPagedList(pageNumber,pageSize));
}
任何协助都将不胜感激。这可能吗?

编辑

SELECT dbo.CustomerName.CustomerNameName, dbo.CustomerDetail.CustomerNameID
FROM dbo.CustomerDetail INNER JOIN
     dbo.CustomerName ON dbo.CustomerDetail.CustomerNameID = dbo.Cust
在林克:

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}
关闭

如果要在同一页面上显示详细信息,请修改
Index
操作。创建一个包含客户列表和详细信息对象的模型

检查输入参数是否为空,如果为空,则仅返回客户列表。如果参数不为空,则返回包含客户详细信息的客户列表

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

SELECT dbo.CustomerName.CustomerNameName, dbo.CustomerDetail.CustomerNameID
FROM dbo.CustomerDetail INNER JOIN
     dbo.CustomerName ON dbo.CustomerDetail.CustomerNameID = dbo.Cust
在林克:

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}
关闭

如果要在同一页面上显示详细信息,请修改
Index
操作。创建一个包含客户列表和详细信息对象的模型

检查输入参数是否为空,如果为空,则仅返回客户列表。如果参数不为空,则返回包含客户详细信息的客户列表

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

答案很简单,但我没有领会。在多次运行该程序之后,我注意到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());
    }
//获取:CustomerDetail
公共异步任务索引(int CustomerNameID)
{
IQueryable CustomerDetails=db.CustomerDetails
.其中(c=>c.CustomerNameID==CustomerNameID)
.OrderBy(d=>d.CustomerNameID)
.包括(d=>d.客户名称);
返回视图(等待CustomerDetail.ToListSync());
}

答案很简单,但我没有领会。在多次运行该程序之后,我注意到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());
    }
//获取:CustomerDetail
公共异步任务索引(int CustomerNameID)
{
IQueryable CustomerDetails=db.CustomerDetails
.其中(c=>c.CustomerNameID==CustomerNameID)
.OrderBy(d=>d.CustomerNameID)
.包括(d=>d.客户名称);
返回视图(等待CustomerDetail.ToListSync());
}

当然可以。显示
CustomerDetailController
Index()
方法(尽管我怀疑您确实需要另一种方法(比如
Details(int-ID)
来返回显示详细信息的视图)您的
@Html.ActionLink()
正在调用
索引()
方法的
CustomerDetailController
并将其传递给
CustomerNameID
,但该方法需要参数
字符串排序器、字符串currentFilter、字符串searchString、int?page
。由于您混乱的命名约定,很难理解这一点,但我怀疑它应该是
@Html.ActionLink(item.CustomerName.ToString(),“Detail”,“CustomerName”,new{ID=item.CustomerNameID},…)
当然可以。显示
索引()@Html.ActionLink(item.CustomerNameName.ToString(), "Index", "CustomerDetail", new {customerId = item.CustomerNameID }, null)
        // 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());
    }