Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按id从数据库加载行_C#_Asp.net_Asp.net Mvc_Asp.net Core - Fatal编程技术网

C# 按id从数据库加载行

C# 按id从数据库加载行,c#,asp.net,asp.net-mvc,asp.net-core,C#,Asp.net,Asp.net Mvc,Asp.net Core,我有两种方法尝试根据Id从数据库加载记录。 以下是我的方法: private List<Cliente> GetCLienti(int clienteId) //to load Clients from DB { var tuttiClienti = _db.tboSottoClienti//Not sure if this query works .Where(c => c.Id_cliente == clienteId)

我有两种方法尝试根据Id从数据库加载记录。
以下是我的方法:

private List<Cliente> GetCLienti(int clienteId) //to load Clients from DB 
{
    var tuttiClienti = _db.tboSottoClienti//Not sure if this query works 
              .Where(c => c.Id_cliente == clienteId)
             .FirstOrDefaultAsync();
    //var tuttiClienti = _db.tboClienti.ToList();
    return tuttiClienti;  //I need to return LIST
}

private List<SottoCliente> GetSottoCliente(clienteId)
{
    var tuttiSottoClienti = _db.tboSottoClienti.ToList();
    return tuttiSottoClienti;
}
我的看法是:

<table class="table table-hover">
    <tr>
        <th>Id</th>
        <th>Nome</th>
        <th>Cognome</th>
        <th>Azienda</th>
        <th>Cellulare</th>
    </tr>
    @foreach (GestioneAtivita.Models.SottoCliente sottoCliente in ViewBag.SottoCliente)
    {
        <tr>
            <td>@sottoCliente.Id</td>
            <td>@sottoCliente.Nome</td>
            <td>@sottoCliente.Cognome</td>
            <td>@sottoCliente.Azienda</td>
            <td>@sottoCliente.Cellulare</td>
        </tr>

    }
</table>

<p><b>Cliente List</b></p>

<table class="table table-hover">
    <tr>
        <th>Id</th>
        <th>Code</th>
        <th>Name</th>
        <th>Enrollment No</th>
    </tr>
    @foreach (GestioneAtivita.Models.Cliente cliente in ViewBag.Cliente)
    {
        <tr>
            <td>@cliente.Nome</td>
            <td>@cliente.Cognome</td>
            <td>@cliente.Nome_azienda</td>
            <td>@cliente.Cellulare</td>
        </tr>

    }
</table>  

身份证件
诺姆
同源的
阿齐恩达
蜂窝织品
@foreach(GestioneAtivita.Models.SottoClient视图包中的SottoClient.SottoClient)
{
@SottoClient.Id
@诺姆索托克利特酒店
@苏托克利特
@阿齐恩达索托克利特酒店
@苏托克利特。赛璐珞
}
客户名单

身份证件 代码 名称 注册号 @foreach(ViewBag.Cliente中的GestioneAtivita.Models.Cliente客户) { @顾客,诺姆 @客户 @客户:Nome_azienda @顾客:蜂窝织品 }
最后,这是我将ID从另一个视图传递给控制器的方式:

<td>
   @Html.ActionLink("Sotto Clienti", "CaricaSottoCliente", new { clienteId = item.Id }, new { @class = "btn btn-outline-success" })
</td>

@ActionLink(“Sotto客户端”、“CaricaSottoCliente”、新的{clienteId=item.Id}、新的{@class=“btn btn outline success”})
如何从
GetCLienti(int-clienteId)
方法返回
列表
? 有什么建议吗?提前谢谢

.FirstOrDefaultAsync();将只返回一个结果。 删除它,或者用.ToList()替换它,代码将返回一个项目列表…

.FirstOrDefaultAsync();将只返回一个结果。
删除它,或者用.ToList()替换它,您的代码将返回一个项目列表…

您正在使用的是
FirstOrDefaultAsync()
,但您没有等待它。您的方法都不是
async
,因此将其替换为
FirstOrDefault()
。但是,作为返回类型的方法,您应该像下面一样使用
.ToList()

private List<Cliente> GetCLienti(int clienteId) //to load Clients from DB 
{
    var tuttiClienti = _db.tboSottoClienti//Not sure if this query works 
              .Where(c => c.Id_cliente == clienteId)
              .ToList();
    //var tuttiClienti = _db.tboClienti.ToList();
    return tuttiClienti;  //I need to return LIST
}

您使用的是
FirstOrDefaultAsync()
,但不是
等待它。您的方法都不是
async
,因此将其替换为
FirstOrDefault()
。但是,作为返回类型的方法,您应该像下面一样使用
.ToList()

private List<Cliente> GetCLienti(int clienteId) //to load Clients from DB 
{
    var tuttiClienti = _db.tboSottoClienti//Not sure if this query works 
              .Where(c => c.Id_cliente == clienteId)
              .ToList();
    //var tuttiClienti = _db.tboClienti.ToList();
    return tuttiClienti;  //I need to return LIST
}
这对我很有用:

这里我从_db.tboClienti加载所有数据

private List<Cliente> GetCLienti(int clienteId)
{
    var tuttiClienti = _db.tboClienti
    .Where(c => c.Id == clienteId)
    .ToList();
    return tuttiClienti;
}
这对我很有用:

这里我从_db.tboClienti加载所有数据

private List<Cliente> GetCLienti(int clienteId)
{
    var tuttiClienti = _db.tboClienti
    .Where(c => c.Id == clienteId)
    .ToList();
    return tuttiClienti;
}
 public IActionResult CaricaSottoCliente(int clienteId)
    {
        ViewBag.Cliente = GetCLienti(clienteId);
        ViewBag.SottoCliente = GetSottoCliente(clienteId);
        return View();
    }