C# 如何使用数据库中的数据构建模型对象?
我想从数据库中读取这些值,并将它们输入到C# 如何使用数据库中的数据构建模型对象?,c#,asp.net,sql-server,asp.net-mvc,asp.net-core,C#,Asp.net,Sql Server,Asp.net Mvc,Asp.net Core,我想从数据库中读取这些值,并将它们输入到textboxfor 如何使用数据库中的数据构建Karta模型对象 控制器: [HttpPost] public async Task<ActionResult> PartialTabelaEcp() { // for example: var numerMiesiaca = 1; var numerRoku = 2020; var userName = _httpContextAccessor.HttpCont
textboxfor
如何使用数据库中的数据构建Karta模型对象
控制器:
[HttpPost]
public async Task<ActionResult> PartialTabelaEcp()
{
// for example:
var numerMiesiaca = 1;
var numerRoku = 2020;
var userName = _httpContextAccessor.HttpContext.User.Identity.Name;
// here I write to the start value database (the ones I want to load from the database)
var dbExists = _ecpContext.Karta.FirstOrDefault(f => f.DzMiesiaca == 1 && f.Miesiac == numerMiesiaca && f.Rok == numerRoku && f.Login == userName);
if (dbExists == null)
{
// I've done it, it works for me
}
if (dbExists != null)
{
var nrIdBase = _ecpContext.Karta.FirstOrDefault(f => f.DzMiesiaca == 1 && f.Miesiac == numerMiesiaca && f.Rok == numerRoku && f.Login == userName).Id;
for (int i = 1; i <= liczbaDni; i++)
{
// here I want read this data
// from database and send to textboxfor to partialview
}
}
return PartialView("_TabelaEwidencja" );
}
这是你想要做的事情吗
[HttpPost]
public async Task<ActionResult> PartialTabelaEcp()
{
// for example:
var numerMiesiaca = 1;
var numerRoku = 2020;
var userName = _httpContextAccessor.HttpContext.User.Identity.Name;
// you don't need to check for existing data and then get it in seperate calls to the db, just get a list at once and it will have 0+ elements.
var karta = _ecpContext.Karta.Where(f => f.DzMiesiaca == 1 && f.Miesiac == numerMiesiaca && f.Rok == numerRoku && f.Login == userName).ToList();
var kartaList = new List<Karta_Model>();
if (karta.Any())
{
karta.ForEach(x => {
kartaList.Add(new Karta_Model{
//map properties here
};
});
}
//pass the list to the partial view
return PartialView("_TabelaEwidencja", kartaList );
}
您使用的是
FirstOrDefault()
,因此只获取一行,而不是列表。你的问题一点也不清楚。您在获取数据方面有问题吗?!或者在视图中显示它们时出现问题
?!
@using AppEcp.Models
@model ParentView
@Html.TextBoxFor(m => m.Model1[nr_rows].Rozpoczecie, new { @class = "start", @type = "time" })
@Html.TextBoxFor(m => m.Model1[nr_rows].Zakonczenie, new { @class = "end", @type = "time" })
@Html.TextBoxFor(m => m.Model1[nr_rows].OdbiorGodzin, new { @class = "gethours", @type = "time" })
[HttpPost]
public async Task<ActionResult> PartialTabelaEcp()
{
// for example:
var numerMiesiaca = 1;
var numerRoku = 2020;
var userName = _httpContextAccessor.HttpContext.User.Identity.Name;
// you don't need to check for existing data and then get it in seperate calls to the db, just get a list at once and it will have 0+ elements.
var karta = _ecpContext.Karta.Where(f => f.DzMiesiaca == 1 && f.Miesiac == numerMiesiaca && f.Rok == numerRoku && f.Login == userName).ToList();
var kartaList = new List<Karta_Model>();
if (karta.Any())
{
karta.ForEach(x => {
kartaList.Add(new Karta_Model{
//map properties here
};
});
}
//pass the list to the partial view
return PartialView("_TabelaEwidencja", kartaList );
}
@foreach (var karta in Model) {
@Html.TextBoxFor(m => karta.Rozpoczecie, new { @class = "start", @type = "time" })
@Html.TextBoxFor(m => karta.Zakonczenie, new { @class = "end", @type = "time" })
@Html.TextBoxFor(m => karta.OdbiorGodzin, new { @class = "gethours", @type = "time" })
}