【发布时间】:2020-06-17 12:17:49
【问题描述】:
我正在尝试制作可以管理客户的应用程序。我所有的客户都有他自己的客户,由我管理。所以我为我的客户创建了两个表tboClienti 和tboSottoClienti 为我的“下客户”创建了数据库。
这是表格的结构:
Structure of table Clients - tboClienti 和 structure of table Under Clients - tboSottoClienti。
我正在为我的“下属客户”进行 CRUD 操作,我想制作下拉列表,我可以在其中选择我的客户并为我的下属客户插入信息(姓名、姓氏、公司、电话)。
我为我的下客户端制作了一个控制器模型和视图,但我不知道如何在我的 View Razor 页面中制作列表。
这是控制器:
public class SottoClientiController : Controller
{
private readonly AppDbContext _db;
public SottoClientiController(AppDbContext db)
{
_db = db;
}
public IActionResult Index()
{
var datiSottoClienti = _db.tboSottoClienti.ToList();
return View(datiSottoClienti);
}
public IActionResult CreareLista()
{
ViewData["lstClieti"] = new SelectList(_db.tboClienti, "Id", "Nome_azienda");
return View();
}
public IActionResult CreareSottoCliente()
{
return View();
}
[HttpPost]
public async Task<IActionResult> CreareSottoCliente(SottoCliente sottoCliente)
{
if (ModelState.IsValid)
{
_db.Add(sottoCliente);
await _db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(sottoCliente);
}
}
这是模型类:
public class SottoCliente
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage = "Inserisci il nome di proprietario dell'azienda")]
[Display(Name = "Nome")]
public string Nome { get; set; }
[Required(ErrorMessage = "Inserisci il cognome di proprietario dell'azienda")]
[Display(Name = "Cognome")]
public string Cognome { get; set; }
[Display(Name = "Azienda")]
public string Azienda { get; set; }
[Required(ErrorMessage = "Inserisci il numero di telefono dell'azienda")]
[DataType(DataType.PhoneNumber)]
[Display(Name = "Telefono")]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$",
ErrorMessage = "Numero non valido")]
public string Cellulare { get; set; }
}
我不知道如何从表tboClienti 中获取数据以将其插入列表。所以这样我就可以为我的下属客户选择客户。
这是视图:
<h4>Under Client</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="CreareSottoCliente">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Azienda" class="control-label"></label>
<select asp-for="Azienda" class="form-control" asp-items="@ViewBag.lstClieti"></select>
<span asp-validation-for="Azienda" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Id" class="control-label"></label>
<input asp-for="Id" class="form-control" />
<span asp-validation-for="Id" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Nome" class="control-label"></label>
<input asp-for="Nome" class="form-control" />
<span asp-validation-for="Nome" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Cognome" class="control-label"></label>
<input asp-for="Cognome" class="form-control" />
<span asp-validation-for="Cognome" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Azienda" class="control-label"></label>
<input asp-for="Azienda" class="form-control" />
<span asp-validation-for="Azienda" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Cellulare" class="control-label"></label>
<input asp-for="Cellulare" class="form-control" />
<span asp-validation-for="Cellulare" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
我将连接字符串插入appsettings.json,然后将其添加到ConfigureServices();。
这是数据库上下文的类:
public class AppDbContext: DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options): base (options)
{
}
public DbSet<Tecnico> tboTecnici { get; set; }
public DbSet<Cliente> tboClienti { get; set; }
public DbSet<SottoCliente> tboSottoClienti { get; set; }
}
对如何从数据库中获取数据并将其插入列表有什么建议吗?
提前致谢!
【问题讨论】:
标签: c# asp.net asp.net-mvc entity-framework asp.net-core