asp.net-mvc – 修改模型时更新视图

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 修改模型时更新视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
只是想知道是否可以更新连接到ASP.NET MVC模型的视图.

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

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

> Create.cshtml
> Delete.cshtml
> Details.cshtml
> Edit.cshtml
> Index.cshtml

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

解决方法

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.

请注意,这显然也会覆盖您的控制器.确保备份并使用源代码控制.

原文链接:https://www.f2er.com/aspnet/249529.html

猜你在找的asp.Net相关文章