首页 技术 正文
技术 2022年11月15日
0 收藏 384 点赞 4,915 浏览 2798 个字

MVC里面的强类型视图的确是一个很强大的东西,结合EF(Entity Framework)的话更加强大,可以直接把你的数据库直接生成强视图的增删查改的视图,在视图中所有Model的属性均动态的,我们不必知道它实际的类型,而且又很容使用@的模型关键字来表示模型的类型名称。

MVC 强类型视图

这里的这个类是我自己在新建ViewModel下的一个普通的的类,可以使用EF从数据库生成的类,不过考虑到我的表里面有一些虚拟外键属性,就必须要有个ViewModel来专门生成视图,都一样,其实意思就是我们必须要先有个模型model,比如我上面这个模型。

然后接下来:就是添加视图,在指定位置,这个我不细说了。

MVC 强类型视图

MVC 强类型视图

在上面的我选择了强类型视图,模型类中的就是我刚刚创建的类,支架模板里面有很多模板,有Empty,Details,Edit,Delete,Create,List,这里我选择了Edit,也就是用来编辑我数据库中的记录,然后你就会发现强大的地方来了,MVC自动按照你的要求生成了一个适合你那个数据表的编辑模板,里面也把所有的信息都添加了可编辑的模板

ps:如果这个类是你刚刚创建的话需要去重新生成一下项目才能在模型类中找到它

MVC 强类型视图

其中上面这个@model MODEL.ViewModel.Admin正是来告诉视图,这个视图的model属性到底是个什么属性,然后便可以通过控制器传过来的模型分别的填充下面的强类型空缺

@model MODEL.ViewModel.Admin@{
Layout = null;
}<!DOCTYPE html><html>
<head>
<meta name="viewport" content="width=device-width" />
<title>EditAdmin</title>
</head>
<body>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> @using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true) <fieldset>
<legend>Admin</legend> @Html.HiddenFor(model => model.Id) <div class="editor-label">
@Html.LabelFor(model => model.LoginName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LoginName)
@Html.ValidationMessageFor(model => model.LoginName)
</div> <div class="editor-label">
@Html.LabelFor(model => model.Number)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Number)
@Html.ValidationMessageFor(model => model.Number)
</div> <div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div> <div class="editor-label">
@Html.LabelFor(model => model.Remark)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Remark)
@Html.ValidationMessageFor(model => model.Remark)
</div> @*<div class="editor-label">
@Html.LabelFor(model => model.Json)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Json)
@Html.ValidationMessageFor(model => model.Json)
</div>*@ <div class="editor-label">
@Html.LabelFor(model => model.DepartmentId)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.DepartmentId, ViewBag.departmentList as IEnumerable<SelectListItem>)
@Html.ValidationMessageFor(model => model.DepartmentId)
</div> @*<div class="editor-label">
@Html.LabelFor(model => model.BaseInfo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.BaseInfo)
@Html.ValidationMessageFor(model => model.BaseInfo)
</div>*@ <p>
<input type="submit" value="Save" />
</p>
</fieldset>
} <div>
@Html.ActionLink("Back to List", "Index")
</div>
</body>
</html>

如果这个时候传给视图的模型是空的,也就是为null值,那么打开edit页面的时候里面的值也相应为空白,然后我通过EF简单的从数据库查到了一条数据,并把其转化为ViewModel传给了视图。代码如下

        //根据id 查询要修改的权限
var model = OperateContext.Iqgzx_adminBLL.GetListBy(x => x.Id == id).FirstOrDefault().ToViewModel();
//将权限对象 传给视图的model属性
return PartialView(model);

显示的效果如下:

MVC 强类型视图

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918