欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MVC学习之四_____新增记录 博客分类: MVC4  

程序员文章站 2024-02-04 23:39:40
...
AddInfo模板代码:

@{
    //Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>新增页面</title>
</head>
<body>
    <br /><br />
    <form action="/BasicData/Add" method="post">
        <div class="table">
            <div class="tab_tr">
                <div class="tab-td">名称</div>
                <div class="tab-td"><input name="name" type="text" /></div>
            </div>
            <div class="tab_tr">
                <div class="tab-td">关键字</div>
                <div class="tab-td"><input name="key" type="text" /></div>
            </div>
            <div class="tab_tr">
                <div class="tab-td">值</div>
                <div class="tab-td"><input name="value" type="text"  /></div>
            </div>
            <div class="tab_tr">
                <div class="tab-td">日期</div>
                <div class="tab-td"><input name="create_date" type="text" /></div>
            </div>
        </div>
        <div style="text-align:center">
            <br />
            <input type="submit" value="提交" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="重置" />
        </div>
    </form>
</body>
</html>



列表页模板代码,增加了对新增跳转的导航
@model PagedList<AppBasicData>
@using Webdiyer.WebControls.Mvc;
@{

}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>IndexPage</title>
    <link rel="stylesheet" type="text/css" href="~/Content/BasicData/div_table.css" />
</head>
<body>
    <div> 
        <div class="table">
            <div class="tab_tr">
                    <div class="tab_th">名称</div>
                    <div class="tab_th">key值</div>
                    <div class="tab_th">value值</div>
                    <div class="tab_th">创建时间</div>
                    <div class="tab_th">操作</div>
            </div>
            
                @foreach (var item in Model)
                {
                    <div class="tab_tr">
                        <div class="tab-td">@item.Name</div>
                        <div class="tab-td">@item.Key</div>
                        <div class="tab-td">@item.Value</div>
                        <div class="tab-td">@item.CreateDate</div>
                        <div class="tab-td">@Html.ActionLink("新增", "AddInfo") |@Html.ActionLink("编辑", "Edit",new {key = @item.Key}) |@Html.ActionLink("删除", "Delete", new { key = @item.Key})</div>
                    </div>
                }
        </div>
        <div class="text-right">
            @Html.Pager(Model).Options(o => o.SetPageIndexParameterName("id").SetHorizontalAlign("right").SetPagerItemTemplate("{0}&nbsp;"))
        </div>
    </div>
</body>
</html>



后台代码:
[HttpGet]
        public ActionResult AddInfo()
        {
            return View();
        }

        [HttpPost]
        public void Add()
        {
            string name = Request.Form["name"].ToString();
            string key = Request.Form["key"].ToString();
            string value = Request.Form["value"].ToString();
            string create_date = Request.Form["create_date"].ToString();

            using (var db = new MyDbContext())
            {
                try
                {
                    AppBasicData model = db.AppBasicDatas.Where(x => x.Key == key).SingleOrDefault();
                    if (model != null)
                    {
                        Response.Write("<script>alert('key已存在,请重新输入');self.location='/BasicData/AddInfo';</script>");
                    }
                    else
                    {
                        try
                        {
                            model = new AppBasicData();
                            model.CreateDate = DateTime.Now;
                            model.Name = name;
                            model.Value = value;
                            model.CreateDate = DateTime.Now;
                            model.CreateId = 1;
                            model.DeleteFlag = true;
                            model.ModifyDate = DateTime.Now;
                            model.ModifyId = 1;
                            model.Key = key;
                            db.AppBasicDatas.Add(model);
                            db.SaveChanges();
                            Response.Write("<script>alert('添加成功');self.location='/BasicData/IndexPage';</script>");
                        }
                        catch(Exception e)
                        { Response.Write("<script>alert('添加失败');self.location='/BasicData/IndexPage';</script>"); }
                    }
                }
                catch
                {
                    Response.Write("<script>alert('新增失败');self.location='/BasicData/IndexPage';</script>");  //给提示后,重定位到列表页
                }
            }
        }


备注:新增和编辑 页面重合度很高,可以将两个页面合并