0
我有一个简单的实体框架4.1的asp.net mvc3项目。 我问了一个以前的问题,答案效果很好 - How to correctly override the SaveChanges function in EF 4.1 date和loggedby的值是在类的构造函数中设置的。这对于创建操作来说很好,但是当我使用编辑操作时,我看到日期和LoggedBy字段发生了变化,这是我不想要的。我也看到删除不起作用 - 我猜这是因为构造函数。 我该如何解决这个问题,以便将Date和LoggedBy值设置为Create,但在编辑过程中没有更改? B.ASP.NET MVC3需要更改Web应用程序的默认字段
using System;
using System.Collections.Generic;
using System.Web;
public partial class tblDefectDetail
{
public tblDefectDetail()
{
DateLogged = DateTime.Now;
LoggedBy = HttpContext.Current.User.Identity.Name;
}
public int ID { get; set; }
public int Project { get; set; }
public string LoggedBy { get; set; }
public System.DateTime DateLogged { get; set; }
public string Issue { get; set; }
我的行动看起来是这样的:
public ActionResult Create()
{
ViewBag.BugLocation = new SelectList(db.tblBugLocations, "ID", "BugLocation");
ViewBag.BugType = new SelectList(db.tblBugTypes, "ID", "BugType");
ViewBag.BusinessUnit = new SelectList(db.tblBusinessUnits, "ID", "BU_Name");
ViewBag.BugFoundMethod = new SelectList(db.tblDefectDiscoveryMethods, "ID", "DiscoveryMethod");
ViewBag.Project = new SelectList(db.tblProjects, "ID", "ProjectName");
ViewBag.Language = new SelectList(db.tblLanguages, "ID", "Language");
return View();
}
感谢您的建议,并很抱歉没有尽快回复您。这似乎是要走的路。但是,我目前的Action方法如下所示。当我添加你的代码时,我得到了一个异常。我应该如何修改它? – bearaman
Code for Action Create()添加到原始文章。 – bearaman
@bearaman - 我的代码是一个例子,你需要弄清楚如何最好地使它为你工作。你真的应该使用视图模型而不是在ViewBag中传递所有东西。 –