簡體   English   中英

如何在 NET Core 中使用通用列表對通用 Class 進行 CRUD?

[英]How to CRUD a Generic Class with Generic List in NET Core?

假設我有一個像這樣的 class

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime BirthDate { get; set; }
    public List<Work> Employments { get; set; }
}

public class Work
{
    public string Employer { get; set; }
    public int Salary { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}

我的 controller 有 HttpGet 和 httpPost 進行編輯

[HttpGet]
public IActionResult Edit(int id)
{
    var _item = new Person()
    {
        Id = id,
        Name = "Bob",
        BirthDate = new DateTime(1970, 3, 4),
        Employments = new List<Work>()
        {
            new Work(){ Employer="Microsoft", Salary=5000, StartDate= new DateTime(1998, 1, 1), EndDate=new DateTime(2005, 12, 31)},
            new Work(){ Employer="Google", Salary=6000, StartDate= new DateTime(2006, 1, 1), EndDate=new DateTime(2013, 12, 31)},
            new Work(){ Employer="Spotify", Salary=10000, StartDate= new DateTime(2014, 1, 1)}
        }
    };
    return View(_item);
}

[HttpPost]
public IActionResult Edit(Person _item)
{
    return View(_item);
}

我的 Edit.csthml 看起來像

@Model  Person

<form asp-action="Edit">
    <div class="form-group">
        <label asp-for="Name" class="control-label"></label>
        <input type="string" asp-for="Name" class="form-control" />
        <span asp-validation-for="Name" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label asp-for="BirthDate" class="control-label"></label>
        <input type="date" asp-for="BirthDate" class="form-control" />
        <span asp-validation-for="BirthDate" class="text-danger"></span>
    </div>

    <GRID>
        table with the rows of Work
    </GRID>
    <button type="submit" class="btn-primary">Send</button>
</form>

我不知道如何獲取 Grid 部分,我看到的所有示例都是獨立的網格或有自己的 AJAX 請求,而不是其他屬性的一部分。

在舊的 asp.net WebForms 中,您可以在一次更新中獲得它。

那么你如何在 .NET Core 中做這樣的事情呢?

問題不是數據庫更新的一部分,我使用 Dapper 和存儲過程,而不是 EF Core ...

問題是如何在數據庫更新之前將編輯后的數據檢索到我的通用 class ......

您想在編輯視圖上更新人員和相應的工作數據嗎?

首先core 中沒有 Grid 標簽,可以使用原有的table tag來展示表格。

並且還需要將Person class的Id鍵字段放入表單中,並使用hidden控件將其隱藏,保證Id字段的值也可以傳遞。

為保證所有更新的數據都能傳遞到 controller 中,需要work class 對應的控件中添加name屬性,並確保它們的格式為Employs[index].FieldName

更多細節,請參考以下示例:

@model Person
@{
    var i = 0;
}
<form asp-action="Edit">
    <input type="hidden" asp-for="Id" class="form-control" />
    <div class="form-group">
        <label asp-for="Name" class="control-label"></label>
        <input type="text" asp-for="Name" class="form-control" />
        <span asp-validation-for="Name" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label asp-for="BirthDate" class="control-label"></label>
        <input type="date" asp-for="BirthDate" class="form-control" />
        <span asp-validation-for="BirthDate" class="text-danger"></span>
    </div>
    <div class="form-group">
        <table>
            <tr>
                <th>Employer</th>
                <th>Salary</th>
                <th>StartDate</th>
                <th>EndDate</th>
            </tr>
            @foreach (var item in Model.Employments)
            {
                <tr>
                    <td>
                        <input type="text" asp-for="@item.Employer" name="Employments[@i].Employer" class="form-control" />
                        <span asp-validation-for="@item.Employer" class="text-danger"></span>
                    </td>
                    <td>
                        <input type="number" asp-for="@item.Salary" name="Employments[@i].Salary" class="form-control" />
                        <span asp-validation-for="@item.Salary" class="text-danger"></span>
                    </td>
                    <td>
                        <input type="date" asp-for="@item.StartDate" name="Employments[@i].StartDate" class="form-control" />
                        <span asp-validation-for="@item.StartDate" class="text-danger"></span>
                    </td>
                    <td>
                        <input type="date" asp-for="@item.EndDate" name="Employments[@i].EndDate" class="form-control" />
                        <span asp-validation-for="@item.EndDate" class="text-danger"></span>
                    </td>
                </tr>
                i++;
            }
        </table>
    </div>
    <button type="submit" class="btn-primary">Send</button>
</form>

這是測試結果:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM