[英]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
的数据,因此在为此方法构建参数时,模型绑定器将值CreatedAt
为null
。
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:
所以你基本上有两个选择:
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.