【问题标题】:How do I keep track of related record ID's in ASP.Net core for updating child records如何在 ASP.Net 核心中跟踪相关记录 ID 以更新子记录
【发布时间】:2020-05-08 02:35:35
【问题描述】:

我是 asp.net core 的新手和初学者。我正在创建一个应用程序,该应用程序需要一个更新项目的页面,并且它是同一页面上的子项目。更新父数据很容易,但我对如何更新子项有点迷茫。

这是项目和子项目的简化模型:

public class Test
{
    public int ID { get; set; }
    public string Name { get; set; }
    public ICollection<TestChild> TestChildren { get; set; }
}

public class TestChild
{
    public int ID { get;set; }
    public string ChildName { get;set; }
    public int TestID { get;set; }
    public Test Test {get; set; }
}

我在 Razor 网页上使用与此类似的代码将其呈现给用户:

...
    <input asp-for="Test.Name" class="form-control" />
    @foreach (var child in Model.Test.TestChildren)
    {
        <input name="TC.@child.ID" value="@child.ChildName">
    }
...

我的计划是使用子输入框中的名称字段来返回每个子记录的 ID,这与值字段一起可以让我更新正确的记录。这意味着当表单发布时,返回的数据可能类似于以下内容:

TC.4 = "Child 1"
TC.6 = "Child 2"
TC.9 = "Child 3"

.X 是相关子项的 ID,名称是它的新/更新名称。我真正想要的是以某种方式返回一个字典,其中键是 ID,值是名称。

但是,如果我在发布表单时使用上面的代码,我不知道将变量声明为什么类型,它会按照下面的方式传递给 onpost 函数:

public async Task<IactionResult> OnPostAsync(int id, ???????????)
{
    ...
    What variable type should I use to map to the data above?
} 

所以我的问题是:

  1. 这是更新子记录的最佳方式吗?
  2. 它们是链接子记录 ID 和值并将其传递回 onpost 函数的更简单/更好的方法吗?
  3. 如果我所拥有的是正确的,我必须像在 onpost 中那样声明变量吗?

感谢您的帮助

【问题讨论】:

    标签: c# entity-framework asp.net-core razor


    【解决方案1】:

    我认为这不是最好的方法。

    你不需要在post方法中添加receive参数。因为您将Test 数据传递给页面,所以您后面的代码应该有一个Test 类型的字段,对吧?

    您只需要确保在提交视图时模型是Test 类型。您需要确保the name of children is consistent with the hierarchical relationship of the model

    您需要在视图中add the ID field as a hidden field 以确认您正在更新哪些数据。

    代码如下:

     public class IndexModel : PageModel
        {
            private readonly MydbContext _context;
    
            public IndexModel (MydbContext context)
            {
                _context = context;
            }
            [BindProperty]
            public Test Test { get; set; }
            public void OnGet()
            {
                // here is an example to pass data to view.
                Test = _context.Test.Include(x => x.TestChildren).Where(x => x.ID == 5).FirstOrDefault();
            }
    
            public async Task<IActionResult> OnPostAsync()
            {
              //the Test parameter will change which is contains the value you changed.
               var newtest =   _context.Test.Include(x => x.TestChildren).Where(x => x.ID == Test.ID).FirstOrDefault();
                newtest.Name = Test.Name;
                newtest.TestChildren = Test.TestChildren;
                _context.SaveChanges();
                return Page();
            }
        }
    

    查看:

    @page
    @model WebApplication1_razor_page.IndexModel
    @{
        ViewData["Title"] = "TestChild";
        Layout = "~/Pages/Shared/_Layout.cshtml";
        var i = 0;
    }
    
    <h1>Index</h1>
    <form method="post">
        <input asp-for="Test.ID" class="form-control" hidden />
        <input asp-for="Test.Name" class="form-control" /> 
        @foreach (var child in Model.Test.TestChildren)
        {
            <input asp-for="@child.ID" name="Test.TestChildren[@i].ID"     hidden/>
            <input asp-for="@child.ChildName" name="Test.TestChildren[@i].ChildName"  />
            i++;
        }
        <input id="Submit1" type="submit" value="update" />
    </form>
    

    这是测试结果:

    【讨论】:

    • 非常感谢您为我解决了问题!
    猜你喜欢
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 2011-07-23
    • 2012-07-02
    • 2011-05-06
    • 2011-03-02
    • 2013-07-11
    • 2013-10-31
    相关资源
    最近更新 更多