C#Mvc 添加删除修改

些许内容:Droplets数据库名称

     LanMu2:二级栏目表

       LanMu1:一级栏目表

       WenZhang:文章表

     LanMu2_Id:文章表的外键

View视图部分:

@model Droplets.Models.WenZhang
@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Edit</title>
<link href="~/Content/easyui.css" rel="stylesheet" />
<link href="~/Content/icon.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.easyui.min.js"></script>
<script src="~/Scripts/Ueditor/ueditor.config.js"></script>
<script src="~/Scripts/Ueditor/ueditor.all.min.js"></script>
<script src="~/Scripts/Ueditor/lang/en/en.js"></script>
<script src="/uploadify/jquery.uploadify.min.js" type="text/javascript"></script>
<link href="/uploadify/uploadify.css" rel="stylesheet" />
<script type="text/javascript">
$(function() {//实现上传缩略图
$('#file_upload').uploadify({
'swf': '/uploadify/uploadify.swf',
'fileSizeLimit': '300KB',
'uploader': '/Ajax/Upload',
'fileTypeExts': '*.gif; *.jpg; *.png; *.bmp', 
'multi':false,
'onUploadSuccess': function (file, data, response) {
//alert('The file ' + file.name + ' was successfully uploaded with a response of ' + response + ':' + data);
var result = eval( '('+ data +')' );
if (result.success == 1)
{
$('#picture').attr('src', result.fullName);
$('#PictureCaptions').val(result.fullName);
}
}
});
});
var ue = UE.getEditor('Content', {
initialFrameWidth: 750,
initialFrameHeight: 450,
});
</script>
<style type="text/css">
.td1 {100px }

</style>

</head>
<body>
<div>
<form id="form1" method="post" action="/WenZhang/EditPost">
@Html.HiddenFor(t => t.Id) //隐藏字段,读取Id,Id位自动增长列,不加则无法读取
<table style="margin-top:30px;margin-bottom:30px;">
<tbody>
<tr>
<td class="td1"><label>文章标题<span style="color:red">*</span>:</label></td>
<td>@Html.TextBoxFor(t => t.Title, new { @class = "easyui-textbox", style="height:50px;700px", required = "true"})</td>
</tr>
<tr>
<td class="td1">标题图片</td>
<td>
@Html.HiddenFor(t=>t.PictureCaptions)
<img id="picture" src="@Model.PictureCaptions" alt="" style="200px;height:150px;" />
<div>
<input type="file" name="file_upload" id="file_upload" />
</div>
</td>
</tr>
@*<tr>
<td><label>添加人:</label></td>
<td>@Html.TextBoxFor(t => t.Editor, new { @class = "easyui-textbox" })</td>
</tr>*@
<tr>
<td class="td1">所属目录</td>
<td>@Html.DropDownList("LanMu2_Id", ViewData["ID"] as SelectList, new { @class = "easyui-combobox",style="height:50px;700px;"})</td>
</tr>
<tr>
<td class="td1"><label>内容<span style="color:red">*</span>:</label></td>
<td>
@Html.TextAreaFor(t => t.Content)
@*<textarea id="Content" class="easyui-validatebox" name="Content" required="true" novalidate="true"></textarea>*@
</td>
</tr>
</tbody>
</table>
<div style="margin-left:350px">
<input type="submit" value="提交" style="60px;height:30px;margin-right:50px" />
<input type="reset" value="取消" style="60px;height:30px" />
</div>
</form>
</div>
</body>
</html>

Controller部分:

 public ActionResult Edit(int id)
        {
            var admin = MyWebApp.checkLogin();
            if (admin == null)
            {
                return RedirectToAction("/Managemnet/Login");//判断当前用户是否登录,若未登录跳转登录界面
            }
            //Editor theeditor = PrepairEditor("Message", delegate (Editor editor)
            //{
            //    //set editor properties here
            //});

            WenZhang wenzhang;
            if (id > 0)
            {
                DropletsEntities dao = new DropletsEntities();
                var q = from t in dao.WenZhang where t.Id == id select t;
                wenzhang = q.FirstOrDefault();
                //ViewBag.text = wenzhang.Content;
            }
            else

                wenzhang = new WenZhang();

            DropletsEntities dao1 = new DropletsEntities();
            var list = (from t in dao1.LanMu2 select new { Id = t.Id, Name = t.Name }).ToList();
            if (id == 0)//下拉列表,若Id=0,怎默认显示第一个
            {
                //4个参数:1数据,2值字段,3显示字段,4选中值
                ViewBag.LanMu2_Id = new SelectList(list, "Id", "Name", 1);
            }
            else//否则,显示对应Id的那一个
            {
                DropletsEntities newent = new DropletsEntities();
                var q = from t in newent.LanMu2 where t.Id == wenzhang.LanMu2_Id select t;
                LanMu2 e = q.FirstOrDefault();
                ViewBag.LanMu2_Id = new SelectList(list, "Id", "Name", e.Id);
            }
            return View(wenzhang);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditPost(WenZhang wenzhang)
        {
            DropletsEntities dao = new DropletsEntities();
            //Editor theeditor = PrepairEditor("Message", delegate (Editor editor)
            //{
            //    //set editor properties here
            //});
            if (wenzhang.Id == 0)
            {
                wenzhang.Editor = MyWebApp.currentUser.Name;
                wenzhang.CreateTime = DateTime.Now;
                //wenzhang.Content = theeditor.Text;
                dao.WenZhang.AddObject(wenzhang);
            }
            else
            {
                var temp = dao.WenZhang.FirstOrDefault(t => t.Id == wenzhang.Id);
                temp.Title = wenzhang.Title;
                temp.Editor = wenzhang.Editor;
                temp.Content = wenzhang.Content;
                temp.LanMu2_Id = wenzhang.LanMu2_Id;
                temp.PictureCaptions = wenzhang.PictureCaptions;
            }
            
            dao.SaveChanges();
            dao.Dispose();
            return RedirectToAction("Edit", new { id = wenzhang.Id });
        }
        [HttpPost]
        public ActionResult Delete(int id)
        {
            DropletsEntities dao = new DropletsEntities();
            var q = dao.WenZhang.FirstOrDefault(t => t.Id ==id);
            dao.WenZhang.DeleteObject(q);
            dao.SaveChanges();
            return RedirectToAction("Index");
        }
原文地址:https://www.cnblogs.com/DotaSteam/p/5435329.html