NSun.MVC CRUD操作

系统 1900 0

上一篇介绍 NSun.MVC配置以及简单使用 ,这篇介绍NSun.MVC的增删查改。其中数据操作应用到了NSun.Core.dll前面有介绍。

1、表及字段描述:

    TABLENAME = "enumus";//表名
    
private Int32 _id;//编号
private String _name;//姓名
private Int32 _age;//年龄
private Int32 _sex;//性别
     
  
    2、页面编写:
  
    R.aspx//读取页面
  
    <%@ Page Language="C#" Inherits="NSun.MVC.MVCPage" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <a href="/CRUDView/C.aspx">添加</a><br />
    <%foreach (var item in (System.Collections.Generic.List<NSunMVCSample.EnumusInfo>)ViewData["data"])
      {%>
    <%=item.Id %>
    <%=item.Name %><br />
    <%=item.Age %><br />
    <%=item.Sex %><br />
    <a href="CRUD.do/Entity?id=<%=item.Id %>">修改</a>
    <a href="CRUD.do/Del?id=<%=item.Id %>">删除</a><br />
    <%} %>
</body>
</html>
     
  
    C.aspx//添加页面
  
    <%@ Page Language="C#" Inherits="NSun.MVC.MVCPage" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <form action="CRUD.do/Add" method="post">
    name:<input id="Text1" name="name" type="text" /><br />
    age:<input id="Text2" name="age" type="text" /><br />
    sex:<input id="Text3" name="sex" type="text" />
    <input id="Submit1" type="submit" value="提交" />
    </form>
</body>
</html>
     
  
    U.aspx//修改页面
  
    <%@ Page Language="C#" Inherits="NSun.MVC.MVCPage" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <form action="CRUD.do/Modify" method="post">
    <input id="Hidden1" name="id" type="hidden" value='<%=((NSunMVCSample.EnumusInfo)ViewData["info"]).Id %>' />
    name:<input id="Text1" name="name" type="text" value='<%=((NSunMVCSample.EnumusInfo)ViewData["info"]).Name %>' /><br />
    age:<input id="Text2" name="age" type="text" value='<%=((NSunMVCSample.EnumusInfo)ViewData["info"]).Age %>' /><br />
    sex:<input id="Text3" name="sex" type="text" value='<%=((NSunMVCSample.EnumusInfo)ViewData["info"]).Sex %>' />
    <input id="Submit1" type="submit" value="提交" />
    </form>
</body>
</html>
    3、操作Action编写:
  
     public class EnumusAction : BaseAction
    
    {
        public override ActionMapping Execute(ActionFormBean formbean, HttpRequest request, HttpResponse response)
        {
            ViewData["data"] = DBFactory.dbEnumus.SelectToList();
            return new ActionView("r");
        }

        public ActionMapping AddExecute(ActionFormBean formbean, HttpRequest request, HttpResponse response)
        {
            var f = formbean as EnumusForm;
            EnumusInfo info = new EnumusInfo()
            {
                Name = f.name,
                Age = Convert.ToInt32(f.age),
                Sex = Convert.ToInt32(f.sex),
            };
            DBFactory.dbEnumus.Save(info);
            return new ActionView("CRUD.do", Forward.Redirect);
        }

        public ActionMapping ModifyExecute(ActionFormBean formbean, HttpRequest request, HttpResponse response)
        {
            var f = formbean as EnumusForm;
            EnumusInfo info = DBFactory.dbEnumus.SelectToEntity(f.id);
            info.Name = f.name;
            info.Age = Convert.ToInt32(f.age);
            info.Sex = Convert.ToInt32(f.sex);
            DBFactory.dbEnumus.Save(info);
            return new ActionView("CRUD.do", Forward.Redirect);
        }

        public ActionMapping EntityExecute(ActionFormBean formbean, HttpRequest request, HttpResponse response)
        {
            string id = request["id"];
            EnumusInfo info = DBFactory.dbEnumus.SelectToEntity(id);
            ViewData["info"] = info;
            return new ActionView("u");
        }

        public ActionMapping DelExecute(ActionFormBean formbean, HttpRequest request, HttpResponse response)
        {
            string id = request["id"];
            DBFactory.dbEnumus.Delete(id);
            return new ActionView("CRUD.do", Forward.Redirect);
        }
    }

4、表单实体://这里没有做表单参数验证:)

      public class EnumusForm : ActionFormBean
    
    {
        public string id { get; set; }
        public string name { get; set; }
        public string age { get; set; }
        public string sex { get; set; }
        public override ActionMapping Validate(HttpRequest request)
        {
            return null;
        }
    }
    5、web.config配置action:
  
      <action class="NSunMVCSample.CRUD.EnumusAction,NSunMVCSample" path="/CRUD.do" formbeanclass="NSunMVCSample.CRUD.EnumusForm,NSunMVCSample" id="crud">      
    
      <forward path="~/CRUDView/C.aspx" id="c" type="Transfer"></forward>
      <forward path="~/CRUDView/R.aspx" id="r" type="Transfer"></forward>
      <forward path="~/CRUDView/U.aspx" id="u" type="Transfer"></forward>
    </action>
    6、Finish!:)
  
     
  
    7、附上demo源码下载:
    
      NSunMVCSample.rar
    
  
     
  

NSun.MVC CRUD操作


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论