16

Just wondering if it is possible to update views connected to a model in ASP.NET MVC.

I am using Entity Framework database first which creates a .edmx object model for the solution.

I have created a new scaffolding model based on the model, which created:

  • Create.cshtml
  • Delete.cshtml
  • Details.cshtml
  • Edit.cshtml
  • Index.cshtml

If I add a property in the model, how can I update those views automatically with the new property to represent the updated model in the UI?

1 Answers1

14

As explained in Changing the Database : The Official Microsoft ASP.NET Site:

To update the views you have two options - you can either re-generate the views by once again adding scaffolding for the Student class, or you can manually add the new property to your existing views. In this tutorial, you will add the scaffolding again because you have not made any customized changes to the automatically-generated views. You might consider manually adding the property when you have made changes to the views and do not want to lose those changes.

To ensure the views are re-created, first delete the Student folder under Views. Then, right-click the Controllers folder and add scaffolding for the Student model. Again, name the controller StudentController. When you click Add, you will be asked if you want to replace the existing file named StudentController. Select OK.

The views now contain the [added] MiddleName property.

Note this will apparently also overwrite your controller. Be sure to have a backup and use source control.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Thanks CodeCaster. I was hoping there might be a solution to just modify the elements that have already been create. Mainly because I now need to go through 5 files and add it individual , as i have customized a lot of the elements. –  Jan 13 '14 at 11:01
  • 1
    @gerdi I understand, but as far as I know there is no way to do so. I haven't found the scaffolding extremely useful. If you work with [DisplayTemplates and EditorTemplates](http://stackoverflow.com/questions/5497183/how-to-create-custom-editor-display-templates-in-asp-net-mvc-3) you can have your model properties named in only two places instead of five. – CodeCaster Jan 13 '14 at 11:03