Annotating properties on a model with default values

后端 未结 5 1982
说谎
说谎 2020-12-01 05:40

I created an EF4.1 code-first model (may or may not be important), and I\'m trying to get default values for my Create scaffold template. My model looks like:



        
5条回答
  •  既然无缘
    2020-12-01 05:58

    Assuming that your view has a definition such as:-

    @model Person
    

    and your controllers HTML GET returns an empty View

    return View();
    

    Then simply add a class that can be rendered that contains default values

    return View(new Person{ Age = 18 });
    

    Another option is to add a singleton static helper to your Person class that returns a default, populated class

      static public Person GetDefaultNew()
            {
                return new Person{ Age = 18 };
            }
    

    Then you need

    return View(new Person.GetDefaultNew());
    

提交回复
热议问题