NSun.MVC CRUD操作

上一篇介绍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
 
posted @ 2010-08-23 21:51  Dacey  Views(549)  Comments(3Edit  收藏  举报