我的解决办法是从保罗的要求有点不同。
在文件DynamicData \ PageTemplates \ Insert.aspx.cs我做了修改,以显示我的任何表中的新记录了相同的字段默认值。用户仍然可以插入其他东西。
public partial class Insert : System.Web.UI.Page
{
protected MetaTable table;
protected void Page_Init(object sender, EventArgs e)
{
table = DynamicDataRouteHandler.GetRequestMetaTable(Context);
var values = table.GetColumnValuesFromRoute(Context);
// set default values for meta data of new records across all tables
// unknown values will be skipped
values.Add("creationDate", DateTime.Now);
values.Add("modificationDate", DateTime.Now);
values.Add("modificationUser", HttpContext.Current.User.Identity.Name.Substring(
HttpContext.Current.User.Identity.Name.IndexOf("\\") + 1));
FormView1.SetMetaTable(table, values);
DetailsDataSource.EntityTypeFilter = table.EntityType.Name;
}
[...]
}
与现有的值编辑记录,我更改了一些DynamicData \ FieldTemplates文件。
public partial class Text_EditField : System.Web.DynamicData.FieldTemplateUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
// ...
// show current user as value for the modification user upon editing records
if (Column.Name == "modificationUser")
{
FieldValue = Page.User.Identity.Name.Substring(Page.User.Identity.Name.IndexOf("\\") + 1);
}
}
[...]
}
它会在页面上显示更新后的值进行编辑,但是更新后更改不会持续!需要额外更改编辑页面模板:
protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
// make sure a meta data update is always triggered by setting a different old value
// required for the edit components
if (e.OldValues.Contains("modificationUser"))
{
e.OldValues["modificationUser"] = string.Empty;
e.OldValues["modificationDate"] = DateTime.MinValue;
}
}
更详细的解释你正在做什么:[在使用实体框架插入ASP.NET动态数据网站之前更新值](http://batesits.com/tag/dynamic-data/) – ofthelit 2016-01-26 14:03:14