【问题标题】:Updating records via api通过 api 更新记录
【发布时间】:2021-02-07 18:13:01
【问题描述】:

我刚刚开始使用 Razor Pages 将旧的 .net 框架平台升级到 .net Core 5

我遇到的第一个问题是更新记录。

我们有一个前端和一个后端表单来编辑用户。在前端只有几个字段可见,而在后端我们有更多字段

模型可能看起来像这样

public class User
{
    public int ID {get;set;}
    public string Name {get;set;}
    public int UserType {get;set;}
    public DateTime TStamp {get;set;}
}

在前端,用户可以更新名称,我不想使用隐藏字段公开 UserType(或 TStamp)的值。

但这意味着用户类型和 TStamp 总是被重置

我已经阅读了最好的方法是将模型发送到服务器,然后更新(并验证)服务器端的记录,例如:

Model recordToUpdate = GetRecordFromDB(id)
recordToUpdate.Name = postedRecord.Name;
UpdateRecord(recordToUpdate);
return recordToUpdate

还有其他方法可以只更新少数字段吗?

2021 年 8 月 2 日 11:57 我找到了这个脚本,它遍历一个模型和一个视图模型,然后传输数据。

https://www.codeproject.com/Tips/5163606/Generic-MVVM-Data-Exchange-between-Model-and-ViewM

public enum MVVMDirection { FROM, TO };

/// <summary>
/// ViewModel base class
/// </summary>
public class VMBase
{
    /// <summary>
    /// Move the data from the model to the viewmodel, using reflection. 
    /// Property names in both objects MUST be the same (both name and type)
    /// </summary>
    /// <typeparam name="TModel">The model's type</typeparam>
    /// <param name="model">The model object the data will be moved from</param>
    public void UpdateFromModel<TModel>(TModel model)
    {
        this.Update<TModel>(model, MVVMDirection.FROM);
    }

    /// <summary>
    /// Move the data from the viewmodel to the model, using reflection. 
    /// Property names in both objects MUST be the same (both name and type)
    /// </summary>
    /// <typeparam name="TModel">The model's type</typeparam>
    /// <param name="model">The model object the data will be moved from</param>
    public void UpdateToModel<TModel>(TModel model)
    {
        this.Update<TModel>(model, MVVMDirection.TO);
    }

    /// <summary>
    /// Update to or from the model based on the specified direction. Property names in both 
    /// objects MUST be the same (both name and type), but properties used just for the view 
    /// model aren't affected/used.
    /// </summary>
    /// <typeparam name="TModel">The model's type</typeparam>
    /// <param name="model">The model object the data will be moved to/from</param>
    /// <param name="direction">The direction in which the update will be performed</param>
    public void Update<TModel>(TModel model, MVVMDirection direction)
    {
        PropertyInfo[] mProperties = model.GetType().GetProperties();
        PropertyInfo[] vmProperties = this.GetType().GetProperties();
        foreach (PropertyInfo mProperty in mProperties)
        {
            PropertyInfo vmProperty = this.GetType().GetProperty(mProperty.Name);
            if (vmProperty != null)
            {
                if (vmProperty.PropertyType.Equals(mProperty.PropertyType))
                {
                    if (direction == MVVMDirection.FROM)
                    {
                        vmProperty.SetValue(this, mProperty.GetValue(model));
                    }
                    else
                    {
                        vmProperty.SetValue(model, mProperty.GetValue(this));
                    }
                }
            }
        }
    }

【问题讨论】:

  • 如果TStamp 是一个并发字段,您应该将它传递给用户并返回以确保没有其他人更改数据库记录。通常,您会定义一个视图模型,其中包含允许视图更改的字段。也许使用 AutoMapper 复制到/从数据库记录。
  • 这取决于你的组织。

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


【解决方案1】:

如果您使用的是 EF Core,您可以通过这种方式告诉它哪些属性必须更新:

var user = new User{ ID = id, Name = postedRecord.Name};
_dbContext.Employee.Attach(user);
_dbContext.Entry(user).Property(x => x.Name).IsModified = true;
_dbContext.SaveChanges();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多