修改模型时更新视图

时间:2023-01-20 19:37:58

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

只是想知道是否可以更新连接到ASP.NET MVC中的模型的视图。

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

我首先使用Entity Framework数据库,为解决方案创建.edmx对象模型。

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

我已经基于该模型创建了一个新的脚手架模型,该模型创建了:

  • Create.cshtml
  • Create.cshtml
  • Delete.cshtml
  • Delete.cshtml
  • Details.cshtml
  • Details.cshtml
  • Edit.cshtml
  • Edit.cshtml
  • Index.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?

如果我在模型中添加属性,如何使用new属性自动更新这些视图以表示UI中的更新模型?

1 个解决方案

#1


14  

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

如更改数据库:官方Microsoft ASP.NET站点中所述:

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.

要更新视图,您有两个选项 - 您可以通过再次为Student类添加脚手架来重新生成视图,也可以手动将新属性添加到现有视图中。在本教程中,您将再次添加脚手架,因为您尚未对自动生成的视图进行任何自定义更改。您可以考虑在对视图进行更改后手动添加属性,并且不希望丢失这些更改。

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.

要确保重新创建视图,请首先删除“视图”下的“学生”文件夹。然后,右键单击Controllers文件夹并为Student模型添加scaffolding。再次,将控制器命名为StudentController。单击“添加”时,系统将询问您是否要替换名为StudentController的现有文件。选择确定。

The views now contain the [added] MiddleName property.

视图现在包含[已添加] MiddleName属性。

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

请注意,这显然也会覆盖您的控制器。一定要备份并使用源代码控制。

#1


14  

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

如更改数据库:官方Microsoft ASP.NET站点中所述:

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.

要更新视图,您有两个选项 - 您可以通过再次为Student类添加脚手架来重新生成视图,也可以手动将新属性添加到现有视图中。在本教程中,您将再次添加脚手架,因为您尚未对自动生成的视图进行任何自定义更改。您可以考虑在对视图进行更改后手动添加属性,并且不希望丢失这些更改。

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.

要确保重新创建视图,请首先删除“视图”下的“学生”文件夹。然后,右键单击Controllers文件夹并为Student模型添加scaffolding。再次,将控制器命名为StudentController。单击“添加”时,系统将询问您是否要替换名为StudentController的现有文件。选择确定。

The views now contain the [added] MiddleName property.

视图现在包含[已添加] MiddleName属性。

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

请注意,这显然也会覆盖您的控制器。一定要备份并使用源代码控制。