Html.EditorFor Set Default Value

后端 未结 12 1886
夕颜
夕颜 2020-12-13 03:44

Rookie question. I have a parameter being passed to a create view. I need to set a field name with a default value. @Html.EditorFor(model => model.Id) I need to set this in

12条回答
  •  没有蜡笔的小新
    2020-12-13 04:03

    I just did this (Shadi's first answer) and it works a treat:

        public ActionResult Create()
        {
            Article article = new Article();
            article.Active = true;
            article.DatePublished = DateTime.Now;
            ViewData.Model = article;
            return View();
        } 
    

    I could put the default values in my model like a propper MVC addict: (I'm using Entity Framework)

        public partial class Article
        {
            public Article()
            {
                Active = true;
                DatePublished = Datetime.Now;
            }
        }
    
        public ActionResult Create()
        {
            Article article = new Article();
            ViewData.Model = article;
            return View();
        } 
    

    Can anyone see any downsides to this?

提交回复
热议问题