简体   繁体   English

ASP.NET 核心 Razor 页面绑定属性和保存数据 model

[英]ASP.NET Core Razor Page binding properties and preserve data in model

I have a question about model binding in razor pages .net core 3.1.我有一个关于 model 绑定在 razor 页 .net 核心 3.1 的问题。 Lets say I have a model such as this:假设我有一个 model,如下所示:

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

On one of my razor pages I use this model for only the top 4 items as inputs that are bound to those items.在我的 razor 页面之一上,我仅将 model 用于前 4 个项目作为绑定到这些项目的输入。 In my constructor I am initializing this model with data from the database.在我的构造函数中,我使用数据库中的数据初始化这个 model。 On my HTTPPOST I get the model back, but the last 3 items are null (EPRLogin, EPRPWD, and EPRLaunch) because I have another page where the user updates those items.在我的 HTTPPOST 上,我得到了 model,但最后 3 个项目是 null(EPRLogin、EPRPWD 和 EPRLaunch),因为我有另一个页面,用户可以在其中更新这些项目。

Is there a way to preserve the data in that model and only have the binding update the data from the page?有没有办法保留 model 中的数据,并且只让绑定更新页面中的数据? The reason I ask, is because I have a function that I get the model from and I send it to save it to the database.我问的原因是因为我有一个 function,我从中获取 model,然后将其发送以将其保存到数据库中。

The only way I can think of doing this would be to get my model within the save method, change only the items I got back from the page model and then send my modified model to be saved.我能想到的唯一方法是在保存方法中获取我的 model,仅更改我从页面 model 取回的项目,然后发送修改后的 model 进行保存。

I hope I explained this well enough...我希望我解释得足够好......

As you said, I think the best way is to update only the first four items.如您所说,我认为最好的方法是只更新前四项。 Below is a work demo.下面是一个工作演示。

You can add an Id into your model.您可以将 Id 添加到您的 model 中。

Then in your pagemodel(Assuming Id=1 is the initial value):然后在你的页面模型中(假设 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");
    }
}

And your page:还有你的页面:

<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>

Result:结果: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM