【问题标题】:How constructing the Model objects with data from the database?如何使用数据库中的数据构建模型对象?
【发布时间】: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


【解决方案1】:

这是否符合您想要做的事情?

[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 );
}

将您的部分模型更改为 List() 并像这样遍历列表:

@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" })
}

【讨论】:

    猜你喜欢
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    相关资源
    最近更新 更多