【问题标题】:ASP.NET Core Razor Page binding properties and preserve data in modelASP.NET Core Razor 页面绑定属性并在模型中保留数据
【发布时间】:2021-01-25 20:21:22
【问题描述】:

我对 razor pages .net core 3.1 中的模型绑定有疑问。假设我有一个这样的模型:

    public Boolean bEmailMessages { get; set; }
    public string sUserEmail { get; set; }
    public Boolean bIcal { get; set; }
    public Boolean bEventNotice { get; set; }

    public string EPRLogin { get; set; }
    public string EPRPWD { get; set; }
    public string EPRLaunch { get; set; }

在我的一个 razor 页面上,我只将此模型用于前 4 个项目作为绑定到这些项目的输入。在我的构造函数中,我使用数据库中的数据初始化这个模型。在我的 HTTPPOST 上,我取回了模型,但最后 3 项为空(EPRLogin、EPRPWD 和 EPRLaunch),因为我有另一个页面,用户可以在其中更新这些项。

有没有办法保留该模型中的数据,并且只让绑定更新页面中的数据?我问的原因是因为我有一个函数可以从中获取模型并将其发送到数据库中。

我能想到的唯一方法是在 save 方法中获取我的模型,只更改我从页面模型返回的项目,然后发送我修改后的模型以进行保存。

我希望我解释得足够好......

【问题讨论】:

  • 你的意思是先获取模型中的前四项,然后将这四项传给更新页面,更新页面还有剩余三项,最后传给数据库?
  • 没有。我有一个如上所示的类,这只是用户设置的数据片段。我有一个剃须刀页面,将此类(模型)作为属性绑定到页面。我在整个页面上都有 字段,这些字段绑定到上面显示的类(模型)。但是我的班级还有其他不属于该页面的项目。因此,当我将 HttpPost 返回到页面时,那些 元素会绑定回我的模型。页面上未绑定的项目为NULL。我想保留那些未绑定在我的班级(模型)中的项目。
  • 你的模型中是否有像 Id 这样的主键?
  • 没有主键。我有一个从数据库中读取所有值并填充模型的函数。我基本上把它像字典一样保存在每个用户的数据库中。因此,当用户登录时,我会在需要时加载这些设置。
  • 我想你只需要在post方法中再次查询模型,然后只更新前四项即可。

标签: c# asp.net-core .net-core razor-pages


【解决方案1】:

正如你所说,我认为最好的方法是只更新前四个项目。下面是一个工作演示。

您可以在模型中添加 Id。

然后在你的页面模型中(假设 Id=1 是初始值):

public IActionResult OnGet()
    {
        Simple = _context.Simples.Find(1);
        return Page();
    }

    public IActionResult OnPost(Simple simple)
    {
        _context.Simples.Attach(simple);
        _context.Entry(simple).Property(x => x.bEmailMessages).IsModified = true;
        _context.Entry(simple).Property(x => x.bEventNotice).IsModified = true;
        _context.Entry(simple).Property(x => x.bIcal).IsModified = true;
        _context.Entry(simple).Property(x => x.sUserEmail).IsModified = true;
        _context.SaveChanges();
        if (!ModelState.IsValid)
        {
            return Page();
        }
        return RedirectToPage("./Index");
    }
}

还有你的页面:

<form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="Simple.Id" />
            <div class="form-group">
                <label asp-for="Simple.bEmailMessages" class="control-label"></label>
                <input asp-for="Simple.bEmailMessages" />
                <span asp-validation-for="Simple.bEmailMessages" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Simple.bEventNotice" class="control-label"></label>
                <input asp-for="Simple.bEventNotice" />
                <span asp-validation-for="Simple.bEventNotice" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Simple.bIcal" class="control-label"></label>
                <input asp-for="Simple.bIcal" />
                <span asp-validation-for="Simple.bIcal" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Simple.sUserEmail" class="control-label"></label>
                <input asp-for="Simple.sUserEmail" class="form-control" />
                <span asp-validation-for="Simple.sUserEmail" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </form>

结果:

【讨论】:

    猜你喜欢
    • 2022-06-11
    • 2020-09-06
    • 2020-04-07
    • 2020-11-27
    • 2021-03-07
    • 2020-11-16
    • 2022-01-25
    • 1970-01-01
    • 2020-02-09
    相关资源
    最近更新 更多