Hi Guys,
I'm trying to show data Auto Complete from another model to another singel view, the Auto Complete not just shown from 1 model and 1 view.
For example is :
I have 2 Tables just call table 1 and table 2
Table 1 |
|
|
|
|
field 1 |
field 2 |
field 3 |
field 4 |
field 5 |
id_tbl1 |
judul |
penulis |
penerbit |
kategori |
Table 2 |
|
field 1 |
field 2 |
id_tbl2 |
kategori |
in Create.cshtml I have form to entry data for insert into table 1 but I need to call the auto complete field kategori data from table 2 and show it to view Create.cshtml.
The problem is in 1 singel view model can't be multiple model just allow 1 model per 1 view. How I can pass the auto complete from table 2 to that singel view(Create.cshtml).
How to do that. Any help could be appriciate.
BukuModel.cs
[Table("Buku")]
public class BukuModel
{
[Key]
[Display(Name ="Kode Buku")]
[Required(ErrorMessage ="Kode Buku tidak boleh kosong")]
public string Kode_Buku { get; set; }
[Display(Name = "Judul Buku")]
[Required(ErrorMessage = "Judul Buku tidak boleh kosong")]
public string Judul { get; set; }
[Display(Name = "Penulis Buku")]
[Required(ErrorMessage = "Penulis Buku tidak boleh kosong")]
public string Penulis { get; set; }
[Display(Name = "Penerbit Buku")]
[Required(ErrorMessage = "Penerbit Buku tidak boleh kosong")]
public string Penerbit { get; set; }
[Display(Name = "Tahun Terbit Buku")]
[Required(ErrorMessage = "Tahun Terbit Buku tidak boleh kosong")]
public string Tahun_Terbit { get; set; }
[Display(Name = "ISBN Buku")]
[Required(ErrorMessage = "ISBN Buku tidak boleh kosong")]
public string ISBN { get; set; }
[Display(Name = "Kategori Buku")]
[Required(ErrorMessage = "Kategori Buku tidak boleh kosong")]
public string Nama_Kategori { get; set; }
}
KategoriModel.cs
[Table("Kategori")]
public class KategoriModel
{
[Key]
[Display(Name = "Kategori ID")]
[Required(ErrorMessage = "Kategori ID tidak boleh kosong")]
public string Kategori_Id { get; set; }
[Display(Name = "Nama Kategori Buku")]
[Required(ErrorMessage = "Nama Kategori Buku tidak boleh kosong")]
public string Nama_Kategori { get; set; }
}
BukuController.cs
//Auto Complete textbox search
[HttpPost]
[Produces("application/json")]
public IActionResult Search()
{
DataContext db = new DataContext();
string term = HttpContext.Request.Query["term"].ToString();
var NamaKategori = db.KategoriBuku.Where(K => K.Nama_Kategori.Contains(term)).Take(10)
.Select(K => new
{
label = K.Nama_Kategori
}).ToList();
return Json(NamaKategori);
}
DataContext.cs
public class DataContext:DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder dbContextOptionsBuilder)
{
var Builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");
var Konfig = Builder.Build();
dbContextOptionsBuilder.UseSqlServer(Konfig["ConnectionStrings:db_perpustakaan"]);
}
public DbSet<KategoriModel> KategoriBuku { get; set; }
}
How to show the AutoComplete Nama_Kategori from [Table("Kategori")] to view Create.cshtml ?