如何使没有编辑器字段的DB字段持久化

时间:2015-11-06 10:53:54

标签: c# asp.net-mvc razor

我有一个以下架构的数据库

    public partial class staging
        {
            public int id { get; set; }
            public System.DateTime createtime { get; set; }
            [DataType(DataType.MultilineText)]
            public string general { get; set; }
            public string articletype { get; set; }
        }

我有两个视图创建和编辑。 在创建视图中,我正在更新常规,但文章类型我正在从控制器更新并保存数据库,如下所示

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(staging staging)
        {

            staging.createtime = DateTime.Now;
            staging.articletype = "Stage";
            try
            {
                db.stagings.Add(staging);
                db.SaveChanges();                
            }
            catch (InvalidOperationException e)
            {
                ViewData["error"] = "An error has occured: " + e.Message;
                return View(staging);
            }

            return RedirectToAction("Details/" + staging.id);
        }

现在我只有一般的编辑字段。 因此,当我进入编辑视图页面时,我看到文章类型具有正确的值,但当它返回到“编辑”的HttpPost时,文章类型被设置为NULL,认为这是因为我没有相同的编辑器字段。现在,当我从编辑控制器更新数据库时,它也会在那里设置为NULL。

那么如何才能使这个值在各个视图中保持不变?我只会在控制器中的Create事件中更新,但我总是希望该值保持不变。

0 个答案:

没有答案