【发布时间】:2020-02-08 06:31:30
【问题描述】:
我想从数据库中读取这些值,并将它们输入到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" );
}
我创建一个列表是因为列表中有超过 30 个元素
型号:
public partial class Karta_Model
{
[Key]
public int Id { get; set; }
public int? NrDay{ get; set; }
public int? NrMonth { get; set; }
public int? NrYear{ get; set; }
public string? Rozpoczecie { get; set; }
public string? Zakonczenie{ get; set; }
public string? OdbiorGodzin{ get; set; }
...
}
public partial class ParentView
{
public List<Karta_Model> Model1 { get; set; }
}
如何读取值并将它们传输到表TextBoxFor.?
查看:
@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" })
【问题讨论】:
-
您使用的是
FirstOrDefault(),因此您只获取极端的一行,而不是列表。你的问题根本不清楚。获取数据有问题吗?!或者在view中显示它们有问题?!
标签: c# asp.net sql-server asp.net-mvc asp.net-core