OA项目总结

来源:互联网 发布:轩辕剑修罗进阶数据 编辑:程序博客网 时间:2024/05/16 12:31

这个项目中用到了html,css,javascript,jq,bootstrap,c#等语言,数据操作用的是MVC自带EF操作语言,非常方便。

1.第一步是在web.config中的<connectionStrings>引入数据库

<add name="gongwenbiaoConnection" connectionString="Data Source=.;Initial Catalog=gongwen;User ID=sa;Password=123456;Persist Security Info=True"   providerName="System.Data.SqlClient" />

2.第2步首先在modl下创建一个类,这个类代表了数据库的列名,然后链接数据库:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MyMVCone.Models
{
     [Table("dengji")]
    public class gongwenbiao
    {
      
           
         /// <summary>
         /// 唯一的ID
         /// </summary>
            public Guid ID { get; set; }
      
         /// <summary>
         /// 题目
         /// </summary>
         public string Title { get; set; }

            /// <summary>
            /// 文号
            /// </summary>
            public string pagenum { get; set; }
            public string Type { get; set; }   //类型
            public string laiwendanwei { get; set; }//来问单位
            public string shouzhibumen { get; set; }//收支部门
            public string huanji { get; set; }//缓急
            public string phase { get; set; }//用于标记数据的种类
            public string miji { get; set; }//密级
       
    }

    public class OFDB : DbContext     //继承DbContext类
    {
        public DbSet<gongwenbiao> dengji { get; set; }  //上面那个类的表
        public OFDB()
            : base("gongwenbiaoConnection")         //引入数据库
        { }

    }
}


所创建的数据库表如下


第二部分:然后是在controllers下创建一个类Incoming类,首先得引入model的类的命名空间。

1.控制器类有很多个动作函数,每个动作函数可以对应一个页面:例如

 public ActionResult fenfa()
        {
         return View();     //对应着fenfa这个视图,当然首先得右击该动作,创建fenfa这个视图先。
        }

        public ActionResult lender()
        {
            return View();
        }

2.同时每个动作函数也可以对应页面中的功能:

第一个例子:例如页面传参数给控制器,要求控制器能返回数据给页面,页面然后再把数据渲染到页面上。

 public ActionResult theDraft(string phase)   //phase这个参数是从页面传来的,至于页面如何传来的下文会解释。
        {
            OFDB ofdb = new OFDB();
            var t = from i in ofdb.dengji
                    where i.phase == phase
                    select i;
            return Json(t);
        }


页面通过函数jq下的函数  $.ajax()传参给控制器:具体如何请看例子:

 function ReadList() {    
        $.ajax({
            type: 'post',
            url: '/Incoming/theDraft',    //指明传递到底动作位置
            dataType: 'json',               //数据类型,
            data: { phase: '收文登记' },     //这就是传给面动作的参数
            success: function (data) {      //传递成功则执行这个函数
                $.each(data, function (index) {
                    $("#dgs").append("<tr class='list_hide' id=" + data[index].ID + "><td><input type='checkbox'/></td><td><a onclick='OpenGW(\"" + data[index].ID + "\")'><span class='glyphicon glyphicon-pencil' ></span>" + data[index].Title + "</a></td><td>" + data[index].pagenum + "</td><td>" + data[index].laiwendanwei + "</td><td>" + data[index].shouzhibumen + "</td><td>" + data[index].Type + "</td><td>" + data[index].miji + "</td><td>" + data[index].huanji + "</td><td>" + data[index].huanji + "</td><td><a onclick='EditorGW(\"" + data[index].ID + "\")'><span class='glyphicon glyphicon-pencil'></span>编辑</a> &nbsp;&nbsp;&nbsp;<a class='dengji' name=" + data[index].ID + "  onclick='DengjiGW(\"" + data[index].ID + "\")' data-toggle='modal' data-target='#myModal'><span class='glyphicon glyphicon-book'></span>登记</a><a class='delete' name=" + data[index].ID + "  onclick='DeleteGW(\"" + data[index].ID + "\")'><span class='glyphicon glyphicon-trash'></span>删除</a></td></tr>");      //动态追加html标记
                });

            }
        });
    }


第二个例子:删除功能:原理通过ID进行删除;点击删除按钮,执行一个动作函数,这个函数获取从页面传来的ID号,然后进行删除:

具体如下:

View中有:

  function DeleteGW(ID) {
        if (confirm("确定要删除公文吗?")) {
            $.ajax({
                type: 'post',
                url: '/Incoming/deleteDraft',
                dataType: 'html',
                data: { ID: ID },
                success: function (data) {
                    if (data == "error") {
                        alert("遇到未知错误,公文删除失败!");
                    }
                    else {
                        location.reload();
                    }
                }
            });
        }
    }


控制器中有:

 public ActionResult deleteDraft()
        {
            try
            {
                Guid ID = Guid.Parse(Request["ID"]);
                OFDB ofdb = new OFDB();
                gongwenbiao draft = ofdb.dengji.Find(ID);
                ofdb.dengji.Remove(draft);
                ofdb.SaveChanges();
            }
            catch
            {
                return Content("error");
            }
            return Content("success");
        }


第三个例子:编辑功能:原理:通过ID获取数据显示,然后修改后保存;点击编辑按钮,执行一个函数,弹出一个新的页面,这个新页面中获取了来自动作函数传来的值,页面渲染值,然后我们进行修改,点击保存,这个页面的数值传给具有保存功能的函数;

具体如下:

View下有

//ID的作用是获取该条数据的ID,然后传给newpagess这个函数

 function EditorGW(ID) {
        window.open("/Incoming/newpagess?ID=" + ID, $("#draft_title").val(), "width=1300,height=650,menubar=no,toolbar=no, status=no,scrollbars=yes");
    }


控制器下有:

 public ActionResult newpagess(string caozuo)
        {
            OFDB ofdb = new OFDB();
            if (caozuo == "新建公文")
            {
                Guid id = Guid.NewGuid();
                gongwenbiao draft = new gongwenbiao
                {
                    phase = "收文登记",
                    ID = id,
                    Title = Request["Title"],
                    Type = Request["Type"]

                };
                ofdb.dengji.Add(draft);
                ofdb.SaveChanges();
                ViewBag.ID = id;
                ViewBag.Type = Request["Type"];
                ViewBag.Title = Request["Title"];
                ViewBag.phase = "收文登记";
             
                return View();

            }

            else
            {

                gongwenbiao draft = ofdb.dengji.Find(Guid.Parse(Request["ID"]));    //获取页面传来的ID,通过ID取得数据,在传给新页面
                ViewBag.ID = draft.ID;
                ViewBag.Title = draft.Title;
                ViewBag.Type = draft.Type;
                ViewBag.phase = draft.phase;
                ViewBag.huanji = draft.huanji;
                ViewBag.laiwendanwei = draft.laiwendanwei;
                ViewBag.miji = draft.miji;
                ViewBag.yuedu = Request["yd"];
                return View();
            }
        }

新页面获取这些数据,便可以用于渲染页面;

修改页面的数据,点击保存按钮,获取该页面的数据,传递给具有保存功能的保存函数,让其保存到数据库:

View有:

  $("#save").bind("click", function () {
            $.ajax({
                url: '/Incoming/SaveDraft',
                type: 'post',
                dataType: 'html',
                data: {
                    ID: $("#ID").val(),
                    Type: $("#kind").val(),
                    miji: $("#classification").val(),
                    title: $("#title").val(),
                    phase: "收文登记",
                    laiwendanwei: $("#laiwendanwei").val(),
                    huanji:$("#huanjichengdu").val()

                },//+"&shopName=" + $("#shopName").attr("id") + "&classification=" + $("#classification").attr("id") + "&title=" + $("#title").attr("id") + "&subjectHeadings=" + $("#subjectHeading").attr("id") + "&organizer=" + $("#organizer").attr("id") + "&ponderance=" + $("#ponderance").attr("id") + "&lordSent=" + $("#lordSent").attr("id") + "&copy="+$("#copy").attr("id"),
                success: function (data) {
                    if (data == "error") {
                        alert("发生未知错误,公文没有正确保存!");
                    }
                    else {
                        alert("保存成功!");
                    }
                }
            });
        });


控制器下有:

  public ActionResult SaveDraft(gongwenbiao draft)
        {
            try
            {
                OFDB ofdb = new OFDB();
                ofdb.dengji.Attach(draft);
                ofdb.Entry<gongwenbiao>(draft).State = System.Data.Entity.EntityState.Modified;
                ofdb.SaveChanges();

            }
            catch
            {
                return Content("error");

            }
            return Content("success");

        }


大概就是这样了。





0 0