zoukankan      html  css  js  c++  java
  • MVC 强类型视图

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

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

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

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

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

    其中上面这个@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>
    View Code

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

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

    显示的效果如下:

  • 相关阅读:
    Prototype的深度探索
    MySQL LIST分区
    CentOS6下Haproxy的安装配置
    haproxy做TCP层的负载均衡
    Shape Control for .NET
    如何通过 HSB 颜色模式构建夜间模式
    使用ICSharpCode.TextEditor制作一个语法高亮显示的XML编辑器
    Roslyn介绍
    信息安全名词
    用彩虹表破解MD5、LM Hash等复杂加密密码
  • 原文地址:https://www.cnblogs.com/xmfdsh/p/3696984.html
Copyright © 2011-2022 走看看