简体   繁体   English

项目更新时,CreatedAt字段变为空值

[英]CreatedAt field turns into null value upon item update

I'm facing a problem when updating items. 我在更新项目时遇到问题。 When a new item is created, the CreatedAt value is populated with the current time & date. 创建新项目时,将使用当前时间和日期填充CreatedAt值。 When I edit an item, the CreatedAt value immediately gets set to null. 编辑项目时,CreatedAt值立即设置为null。 The UpdatedAt value gets set correctly but CreatedAt is set to null in the database. UpdatedAt值已正确设置,但CreatedAt在数据库中设置为null。

Here's the ItemViewModel class: 这是ItemViewModel类:

public class ItemViewModel
{
    public int ItemId { get; set; }
    public string ItemName { get; set; }
    public int UserId { get; set; }
    public string CreatedBy { get; set; }
    public DateTime? CreatedAt { get; set; }
    public string UpdatedBy { get; set; }
    public DateTime? UpdatedAt { get; set; }

    public virtual User User { get; set; }
}

This is the update method I've used: 这是我用过的更新方法:

public void UpdateItem(ItemViewModel viewModel)
{
    item.UpdatedBy = WebSecurity.CurrentUserName;
    item.UpdatedAt = DateTime.Now;
    db.SaveChanges();
}

The following create method populates the CreatedAt value correctly: 以下create方法正确填充CreatedAt值:

public int CreateItem(ItemViewModel viewModel)
{
    var item = new Item();
    viewModel.CopyToItem(item); 

    item.UserId = WebSecurity.CurrentUserId;
    item.CreatedBy = WebSecurity.CurrentUserName;
    item.CreatedAt = DateTime.Now;

    db.Items.Add(item);
    db.SaveChanges();
    return item.ItemId;
}

Is there something I've missed out? 有什么我错过了吗?

You should check for the Entry is Modified or not using EntityState as: 您应该使用EntityState检查条目是否已修改为:

public void UpdateItem(ItemViewModel viewModel)
{
    var item=db.Table.where(x=>x.ID==viewModel.ID).FirstOrDefault();
    item.UpdatedBy = WebSecurity.CurrentUserName;
    item.UpdatedAt = DateTime.Now;
    db.Entry(item).state=EntityState.Modified;
    db.SaveChanges();
}

In this method: 在这个方法中:

public void UpdateItem(ItemViewModel viewModel)
{
    item.UpdatedBy = WebSecurity.CurrentUserName;
    item.UpdatedAt = DateTime.Now;
    db.SaveChanges();
}

Where is viewModel coming from? viewModel来自哪里? (And, come to think of it, what is item ?) Is this an action being invoked by a client-side form? (而且,想一想,什么是item ?)这是一个客户端表单调用的动作吗? I suspect what's happening here is that the client-side form doesn't include the data for CreatedAt , so the model binder leaves the value as null when building the parameter for this method. 我怀疑这里发生的是客户端表单不包含CreatedAt的数据,因此在为此方法构建参数时,模型绑定器将值CreatedAtnull

The model binder isn't "smart enough" to go fetch the current persisted state of the model and merge that with what comes from the form. 模型绑定器不够“足够智能”来获取模型的当前持久状态并将其与来自表单的内容合并。 It only has what comes from the form. 只有来自形式的东西。 So you essentially have two options: 所以你基本上有两个选择:

  1. Persist to the page's form elements all data required to accurately re-construct the model. 坚持页面的表单元素,准确地重新构建模型所需的所有数据。
  2. Persist to the page's form elements at least an identifier for the model and in server-side code use that identifier to re-fetch the state of the model from the persisted data. 至少在页面的表单元素中保留模型的标识符,在服务器端代码中使用该标识符从持久化数据中重新获取模型的状态。

For the first option, you'll need to include CreatedAt . 对于第一个选项,您需要包含CreatedAt Which could be something as simple as: 这可能是一件简单的事情:

@Html.HiddenFor(model => model.CreatedAt)

Try like this 试试这样吧

 var data= db.Items.where(x=>x.ID==viewModel.ID).SingleOrDefault();
    foreach(db.Items in data)
    {
        item.UpdatedBy = WebSecurity.CurrentUserName;
        item.UpdatedAt = DateTime.Now;
    }
     db.SaveChanges();

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

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