MVC学习之四_____新增记录

阅读更多
AddInfo模板代码:

@{
    //Layout = null;
}





    
    新增页面


    

名称
关键字
日期

    



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

}





    
    IndexPage
    


    
名称
key值
value值
创建时间
操作
@foreach (var item in Model) {
@item.Name
@item.Key
@item.Value
@item.CreateDate
@Html.ActionLink("新增", "AddInfo") |@Html.ActionLink("编辑", "Edit",new {key = @item.Key}) |@Html.ActionLink("删除", "Delete", new { key = @item.Key})
}
@Html.Pager(Model).Options(o => o.SetPageIndexParameterName("id").SetHorizontalAlign("right").SetPagerItemTemplate("{0} "))



后台代码:
[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("");
                    }
                    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("");
                        }
                        catch(Exception e)
                        { Response.Write(""); }
                    }
                }
                catch
                {
                    Response.Write("");  //给提示后,重定位到列表页
                }
            }
        }


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

你可能感兴趣的:(MVC学习之四_____新增记录)