首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >现有数据库索引列MVC 4

现有数据库索引列MVC 4
EN

Stack Overflow用户
提问于 2013-05-13 14:35:33
回答 1查看 233关注 0票数 0

我正在使用一个现有的数据库创建一个项目报告系统。我想要主键,它是项目表的Project id值,它不设置新值,因为我必须输入Id,但我不希望用户这样做。有人能帮我解决这个问题吗?

帕特里克

控制器‘/ GET: /Project/Create

代码语言:javascript
复制
    public ActionResult Create()
    {
        ViewBag.Status = new SelectList(db.pjt_Statuses, "pjt_Status_ID", "StatusName");
        ViewBag.SubCategoryID = new SelectList(db.pjt_SubCategories, "pjt_SubCat_ID", "SubCatName");
        return View();
    }

    //
    // POST: /Project/Create

    [HttpPost]
    public ActionResult Create(pjt_Projects pjt_projects)
    {
        if (ModelState.IsValid)
        {
            pjt_projects.CreationDate = DateTime.Now;
            db.pjt_Projects.Add(pjt_projects);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.Status = new SelectList(db.pjt_Statuses, "pjt_Status_ID", "StatusName", pjt_projects.Status);
        ViewBag.SubCategoryID = new SelectList(db.pjt_SubCategories, "pjt_SubCat_ID", "SubCatName", pjt_projects.SubCategoryID);
        return View(pjt_projects);
    }`

数据库表脚本

代码语言:javascript
复制
`CREATE TABLE [dbo].[pjt_Projects](
[Pjt_Id] [int] IDENTITY(1,1) NOT NULL,
[ProjectName] [nchar](100) NULL,
[Status] [int] NULL,
[Start_Date] [date] NULL,
[Estimated_End_Date] [date] NULL,
[Documents] [nvarchar](max) NULL,
[Notes] [nvarchar](max) NULL,
[Budget] [smallmoney] NULL,
[Current_Spending] [smallmoney] NULL,
[ProjectOwner] [nvarchar](50) NULL,
[Active] [bit] NULL,
[Actual_End_date] [date] NULL,
[CreationDate] [date] NULL,
[CreatedByEmpNo] [nchar](10) NULL,
[UpdateDate] [date] NULL,
[UpdatedByEmpNo] [nchar](10) NULL,
[SubCategoryID] [int] NULL,`


**Generate Context Db Code**
    public int Pjt_Id { get; set; }
    public string ProjectName { get; set; }
    public Nullable<int> Status { get; set; }
    public Nullable<System.DateTime> Start_Date { get; set; }
    public Nullable<System.DateTime> Estimated_End_Date { get; set; }
    public string Documents { get; set; }
    public string Notes { get; set; }
    public Nullable<decimal> Budget { get; set; }
    public Nullable<decimal> Current_Spending { get; set; }
    public string ProjectOwner { get; set; }
    public Nullable<bool> Active { get; set; }
    public Nullable<System.DateTime> Actual_End_date { get; set; }
    public Nullable<System.DateTime> CreationDate { get; set; }
    public string CreatedByEmpNo { get; set; }
    public Nullable<System.DateTime> UpdateDate { get; set; }
    public string UpdatedByEmpNo { get; set; }
    public Nullable<int> SubCategoryID { get; set; }    
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-14 20:54:09

只需从Razor视图中排除ID字段即可。(除非您使用的是EditorForModel,否则无需解释,在这种情况下,您需要修改ID字段的Display注释,以便Razor引擎知道将其排除在视图之外。)

一旦排除,它的价值将不会被回发到服务器。这样,当在MVC控制器中创建时,它将获得默认值;数据库将自动生成新的ID。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16524679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档