我正在尝试制作可以管理客户的应用。我所有的客户都有自己管理的客户。因此,我用两个表tboClienti
分别为客户和tboSottoClienti
分别作为我的“下客户”数据库。
这是表的结构:
Structure of table Clients - tboClienti和structure of table Under Clients - tboSottoClienti。
我正在为“下属客户”进行CRUD操作,我想创建一个下拉列表,从中可以选择我的客户并为下属客户插入信息(姓名,姓氏,公司,电话)。
>我制作了一个控制器模型并为我的under客户查看,但是我不知道如何在“查看剃刀页面”中列出列表。
这是控制器:
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; }
}
有人建议如何从数据库中获取数据并将其插入列表吗?
谢谢!
答案 0 :(得分:0)
在ASP.NET Core MVC中,您的下拉列表HTML代码如下所示:
<div class="form-group">
<label asp-for="NewsTypeId" class="control-label"></label>
<select asp-for="NewsTypeId" class="form-control" asp-items="@ViewBag.NewsTypeId"></select>
<span asp-validation-for="NewsTypeId" class="text-danger"></span>
</div>
和在C#控制器中
public IActionResult Create()
{
ViewData["NewsTypeId"] = new SelectList(_context.TypeOfNews, "TypeId", "TypeName");
return View();
}
原始答案:https://stackoverflow.com/a/55843644/3559462
有关逐步操作的详细信息,请查看以下链接:https://www.c-sharpcorner.com/article/binding-dropdown-list-with-database-in-asp-net-core-mvc/
答案 1 :(得分:0)
调试代码,并确保您的ViewData["lstClieti"]
确实存在值。
请确保您的视图名称是否为CreareLista
。如果视图名称为CreareSottoCliente
。
更改如下:
//public IActionResult CreareLista()
//{
// ViewData["lstClieti"] = new SelectList(_db.tboClienti, "Id", "Nome_azienda");
// return View();
//}
public IActionResult CreareSottoCliente()
{
ViewData["lstClieti"] = new SelectList(_db.tboClienti, "Id", "Nome_azienda");
return View();
}
整个演示
1。型号:
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; }
}
public class Cliente
{
public int Id { get; set; }
public string Nome_azienda { get; set; }
}
2。视图名称应为CreareSottoCliente.cshtml
。
3.Controller:
public class SottoClientiController : Controller
{
private readonly MvcProj3_1Context _db;
public SottoClientiController(MvcProj3_1Context db)
{
_db = db;
}
public IActionResult Index()
{
var datiSottoClienti = _db.tboSottoClienti.ToList();
return View(datiSottoClienti);
}
//SottoClienti/CreareSottoCliente
public IActionResult CreareSottoCliente()
{
ViewData["lstClieti"] = new SelectList(_db.tboClienti, "Id", "Nome_azienda");
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);
}
}
4.DbContext:
public class MvcProj3_1Context : DbContext
{
public MvcProj3_1Context (DbContextOptions<MvcProj3_1Context> options)
: base(options)
{
}
public DbSet<Tecnico> tboTecnici { get; set; }
public DbSet<Cliente> tboClienti { get; set; }
public DbSet<SottoCliente> tboSottoClienti { get; set; }
}
5.Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<MvcProj3_1Context>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MvcProj3_1Context")));
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
6.appSettings.json:
"ConnectionStrings": {
"MvcProj3_1Context": "Server=(localdb)\\mssqllocaldb;Database=DatabaseName;Trusted_Connection=True;MultipleActiveResultSets=true"
}