加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 22:44:54 所属栏目:asp.Net 来源:网络整理
导读:只是想知道是否可以更新连接到ASP.NET MVC模型的视图. 我首先使用Entity Framework数据库,为解决方案创建一个.edmx对象模型. 我已经创建了一个基于模型的新的脚手架模型,创建了: Create.cshtml Delete.cshtml Details.cshtml Edit.cshtml Index.cshtml 如果
只是想知道是否可以更新连接到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.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读