发布新闻

1、Models
namespace 发布新闻.Models
{
    public class NewsBF
    {
        private MyDBDataContext _context = new MyDBDataContext();
        public void Insert(string biaoti, string zuozhe, string laiyuan, string zhengwen) //增加
        {
            news data = new news();
            data.title = biaoti;
            data.Author = zuozhe;
            data.source = laiyuan;
            data.content = zhengwen;
            data.time = DateTime.Now;
            _context.news.InsertOnSubmit(data);
            _context.SubmitChanges();
        }
        public List<news> Select()//显示
        {
            return _context.news.ToList();
        }

        public news Select(int id)//显示单个
        {
            var query = _context.news.Where(P => P.newsid == id);
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return null;
            }
        }
        public void Update(int code, string biaoti, string zuozhe, string laiyuan, string zhengwen)//修改
        {
            var query = _context.news.Where(p => p.newsid == code);
            if (query.Count() > 0)
            {
                news x = query.First();
                x.title = biaoti;
                x.Author = zuozhe;
                x.source = laiyuan;
                x.content = zhengwen;            
            }
            _context.SubmitChanges();
        }
        public void Delete(int code) //删除
        {
            var query = _context.news.Where(P => P.newsid== code);
            if (query.Count() > 0)
            {
                news a = query.First();
                _context.news.DeleteOnSubmit(a);

            }
            _context.SubmitChanges();
        }
    }
}

2、Controllsers
namespace 发布新闻.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Insert(string biaoti,string zuozhe,string laiyuan,string zhengwen)
        {
            new NewsBF().Insert(biaoti,zuozhe,laiyuan,zhengwen);
            return RedirectToAction("Index");
        }
        public ActionResult Selcet()
        {
            List<news> list = new NewsBF().Select();
            return View(list);
        }

        public ActionResult Edit(int  id)
        {
            news data = new NewsBF().Select(id);
            return View(data);
        }
        public ActionResult Update(int newsid,string title,string Author,string source,string content)
        {
            new NewsBF().Update(newsid,title,Author,source,content);
            return RedirectToAction("Selcet");
        }
        public ActionResult Delete(int id)
        {
            new NewsBF().Delete(id);
            return RedirectToAction("Selcet");
        }

    }
}

3、Views

Index:
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style>
       
        .aa
         {
             position: relative;
         margin:auto;
        300px;
        height: 400px;
        z-index: 2;
        }
        </style>
</head>
<body>
            <div class="aa">
                <h1 style="text-align:center">发布新闻</h1>                        
                             <form name="f1" id="f1" action="/Home/Insert" method="post">
                                标题:@Html.TextBox("biaoti", "", new { size=35})<br>
                                作者:@Html.TextBox("zuozhe", "")<br>
                                来源:@Html.TextBox("laiyuan", "")<br>
                                内容:@Html.TextArea("zhengwen", "", 6, 55, new { })
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input id="Submit1" type="submit" value="提交" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/Home/Selcet"><input id="Button1" type="button" value="查看" /></a>
                                 </form>                   
                </div>            
</body>
</html>



Edit:

@using  发布新闻.Models;
@model news
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Edit</title>
     <style>
        .aa
         {
             position: relative;
         margin:auto;
        300px;
        height: 400px;
        z-index: 2;
        }
        </style>
</head>
<body>
    <div>
         <div class="aa">
                <h1 style="text-align:center">修改新闻</h1>                        
                             <form name="f1" id="f1" action="/Home/Update" method="post">
                               
                                标题:@Html.TextBoxFor(P=>P.title)<br>
                                作者:@Html.TextBoxFor(P=>P.Author)<br>
                                来源:@Html.TextBoxFor(P=>P.source)<br>
                                内容:@Html.TextBoxFor(P => P.content, new { size=55})<br>
                                 <input type="hidden" id="newsid" name="newsid" value="@Model.newsid"> 
                                 <input id="Submit1" type="submit" value="更新" />
                                 </form>                   
                </div>            
    </div>
</body>
</html>


Select:
@using  发布新闻.Models;
@model List<news>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Selcet</title>
     <style>
        .trhead 
        {
            background-color:#b200ff
            
        }
        .trrow 
        { 
             background-color:#FFFFcc
        }
    </style>
</head>
<body>
    <div>
           <table width="100%" cellpadding="4" cellspacing="1" border="1">
            <tr class="trhead">
                <td>id</td>
                <td>title</td>
                <td>author</td>     
                  <td>source</td>
                  <td>date</td>
                  <td>update</td>
                  <td>delete</td>
               
            </tr>
            @{
                foreach (news data in Model)
                {
               <tr class="trrow">
                
                   <td>@data.newsid</td>
                   <td>@data.title</td>
                   <td>@data.Author</td>
                   <td>@data.source</td>
                   <td>@data.time</td>
                   <td><a href="/Home/Edit/@data.newsid"><img src="~/xiugai.png"></a>&nbsp;&nbsp;</td>
                   <td><a href="/Home/Delete/@data.newsid"><img src="~/shanchu.png"></a> </td>
                       
                      
                </tr>
                }
          
            }
            </table>
    </div>
</body>
</html>

效果图:

原文地址:https://www.cnblogs.com/lk-kk/p/4634104.html