当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET MVC项目中App_Code目录在程序应用

ASP.NET MVC项目中App_Code目录在程序应用

2018年10月29日  | 移动技术网IT编程  | 我要评论

人民币 韩元,口腔软件,李聚奎

学习asp.net mvc,如果你是开发asp.net mvc项目的,也许你去为项目添加前asp.net项目的app_code目录,在这里创建与添加的class类,也许你无法在mvc项目所引用。

那这样说,是不是一没有作用了呢?非也。

从下面一步一步来学习。

创建一个model,名称:machine

 

using system;
using system.collections.generic;
using system.linq;
using system.web;

namespace insus.net.models
{
    public class machine
    {
        public int key { get; set; }

        public string modelnumber { get; set; }

        public string brand { get; set; }

        public datetime manufacturedate { get; set; }
    }
}
source code

 

创建一个entity,名称为machineentity:

 

using system;
using system.collections.generic;
using system.linq;
using system.web;
using insus.net.models;

namespace insus.net.entities
{
    public class machineentity
    {
        public ienumerable<machine> machines()
        {
            return new list<machine>()
            {
                { new machine() {
                    key =1,
                    modelnumber ="tc03q",
                    brand ="qt",
                    manufacturedate = convert.todatetime("2008/12/31")}
                },
                { new machine() {
                    key =2,
                    modelnumber ="mh26u",
                    brand ="hw",
                    manufacturedate = convert.todatetime("2012/03/09")}
                },
                { new machine() {
                    key =3,
                    modelnumber ="dw569",
                    brand ="xm",
                    manufacturedate = convert.todatetime("2015/07/25")}
                }
            };
        }
    }
}
source code

 

创建一个控制器,名称为machinecontroller:

 

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.mvc;

namespace insus.net.controllers
{
    public class machinecontroller : controller
    {
        // get: machine
        public actionresult machinelist()
        {
            return view();
        }
    }
}
source code


最后创建视图显示entity的数据:

 

上面有一行代码#17:

(new machineentity()).

如果一个页面中,多处引用此类,可以把它移至一个语法块中去:
@{......}

 

是时候运行程序,看看效果:

 

 

已经能正常显示数据了,现在,我们需要对时间进行格式化,只想显示日期,去除时间部分:

可以在视图中写一个helper方法:

 

再次显示:

 

程序中,如果这个公共helper方法,我们可以放在一个地方法,当项目中其它视图需要使用时,我们就不必再重新写一遍或者拷贝和粘帖了。

在asp.net mvca项目中,把app_code目录调出来:

 

然后在这个目录添加一个helper cshtml文档:

 

 

这个razor语法的helper文档,名字使用leoyang.cshtml

 

然后修改一个视图的语法,使用到这个公共的helper:

 

这样说明,asp.net mvc项目中的app_code目录,是可以放置一些公共的helper相关。


完毕。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网