MVC新闻提交小练习+Session登录提交练习

控制器端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication9.Models;

namespace MvcApplication9.Controllers
{
    public class NewsController : Controller
    {
        //
        // GET: /News/

        public ActionResult Add()
        {
            if (Session["user"] == null)
            {
                return RedirectToAction("Index", "Login");
            }
            return View();
        }
        public ActionResult Insert(string title,string author,string sourse,string content)
        {
            if (Session["user"] == null)
            {
                return RedirectToAction("Index", "Login");
            }
            DateTime dt = DateTime.Now;
            new NewsDA().Insert(title,author,sourse,content);
            return RedirectToAction("Add");
        }
        public ActionResult Index()
        {
            if (Session["user"] == null)
            {
                return RedirectToAction("Index", "Login");
            }
            List<News> list = new NewsDA().Select();
            return View(list);
        }
        public ActionResult Edit(int newsid)
        {
            if (Session["user"] == null)
            {
                return RedirectToAction("Index", "Login");
            }
            News data = new NewsDA().Select(newsid);
            return View(data);
        }
        public ActionResult Update(int newsid,string title, string author, string sourse, string content)
        {
            if (Session["user"] == null)
            {
                return RedirectToAction("Index", "Login");
            }
            new NewsDA().Update(newsid,title, author, sourse, content);
            return RedirectToAction("Index");
        }
        public ActionResult Delete(int newsid)
        {
            if (Session["user"] == null)
            {
                return RedirectToAction("Index", "Login");
            }
            new NewsDA().Delete(newsid);
            return RedirectToAction("Index");
        }
    }
}

新闻添加页面代码:

@{
    Layout = null;
}
@using MvcApplication9.Models;
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Add</title>
    <style type="text/css">
        .test {
            400px;
        }
        .test1 {
            400px;
            height:350px;
        }
        </style>
</head>
<body>
    <div>
        @using(Html.BeginForm("Insert","News", FormMethod.Post)){
        <h1>新闻发布</h1>
        <div>
          
            <div>
            标题:@Html.TextBox("title", "",new { @class="test"})<br/>
            作者:@Html.TextBox("author", "", new { @class="test"})<br/> 
            来源:@Html.TextBox("sourse", "", new { @class="test"})<br/>
            内容:@Html.TextArea("content", "", new { @class="test"})<br/>   
            <input id="Submit1" type="submit" value="提交" />  <a href="/News/Index"><input id="Button1" type="button" value="查看" /></a> @Html.ActionLink("查看", "Index", "News", new { }, new { })
              
            </div>
    </div>
        }
</body>
</html>

新闻修改视图:

@{
    Layout = null;
}
@model News
@using MvcApplication9.Models;
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Add</title>
    <style type="text/css">
        .test {
            400px;
        }
        .test1 {
            400px;
            height:350px;
        }
        </style>
</head>
<body>
    <div>
        @using(Html.BeginForm("Update","News", FormMethod.Post)){
        <h1>新闻发布</h1>
        <div>
          
            <div>
            @Html.HiddenFor(p=>p.newsid)
            标题:@Html.TextBoxFor(p=>p.title) <br/>
            作者:@Html.TextBoxFor(p=>p.author) <br/> 
            来源:@Html.TextBoxFor(p=>p.sourse) <br/>
            内容:@Html.TextAreaFor(p=>p.content) <br/> 
            @Html.HiddenFor(p=>p.time)  
            <input id="Submit1" type="submit" value="提交" /> 
              
            </div>
    </div>
        }
</body>
</html>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Edit</title>
</head>
<body>
    <div>
        
    </div>
</body>
</html>

新闻主界面视图:

@{
    Layout = null;
}
@using MvcApplication9.Models;
@model List<News>
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style type="text/css">
        
        .tbhead {
            background-color:navy;
            text-align:center;
            color:white;
            font-weight:bold;
        }
        .tbrow {
            text-align:center;
            background-color:#FFFFcc;
        }
    </style>
</head>
<body>
    <div>
        <table id="tbinfo" width="100%" cellpadding="4" cellspacing="1" bgcolor="black" border="0">
            <tr class="tbhead">
                <td>新闻代号</td>
                <td>新闻标题</td>
                <td>作者</td>
                <td>来源</td>
                <td>内容</td>
                <td>时间</td>
            </tr>
  
          @{ 
              foreach( News data in Model)
              {
            <tr class="tbrow">
                <td>@data.newsid</td>
                <td>@data.title</td>
                <td>@data.author</td>
                <td>@data.sourse</td>
                <td>@data.content</td>
                <td>@data.time</td>
                <td>
                    @Html.ActionLink("修改", "Edit", "News", new {newsid=data.newsid}, new { })
                    @Html.ActionLink("删除", "Delete", "News", new {newsid = data.newsid }, new { onclick="return confirm('确认要删除吗?')"})
                </td>
            </tr>
            }
          }
        </table>
        @Html.ActionLink("添加人员", "Add", "News")
    </div>
</body>
</html>

新闻Model层方法代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication9.Models
{
    public class NewsDA
    {
        private newsDataContext _Context = new newsDataContext();
        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();
            }
            return null;
        }
        public void Insert( string til, string aut, string sou, string con)
        {
            News data = new News
            {
                title = til,
                author = aut,
                sourse = sou,
                content = con,
                time = DateTime.Now,
            };
            _Context.News.InsertOnSubmit(data);
            _Context.SubmitChanges();
        }
        public void Delete(int id)
        {
            var query = _Context.News.Where(p => p.newsid == id);
            if (query.Count() > 0)
            {
                News data = query.First();
                _Context.News.DeleteOnSubmit(data);
                _Context.SubmitChanges();
            }
        }
        public void Update(int id,string til, string aut, string sou, string con)
        {
            var query = _Context.News.Where(p => p.newsid == id);
            if (query.Count() > 0)
            {
                News data = query.First();
                data.title = til;
                data.author = aut;
                data.sourse = sou;
                data.content = con;
                //data.time = DateTime.Now;//随时修改修改时间
                _Context.SubmitChanges();
            }
        }
    }
}

登录界面控制器代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication9.Models;

namespace MvcApplication9.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult CheckUidPwd(string uid, string pwd)
        {
            bool isOK = new LoginDA().Login(uid, pwd);
            if (isOK)
            {
                //保存状态
                Session["user"] = uid;
                //跳转界面
                return RedirectToAction("Index", "News");
            }
            else
            {
                return RedirectToAction("Index");
            }
        }
    }
}

登录视图代码:

@using MvcApplication9.Models;
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <h1>登录</h1>
        @using(Html.BeginForm("CheckUidPwd","Login")){
        <div>
            用户名:@Html.TextBox("uid")<br/>
            密码:@Html.Password("pwd")<br/>
            <input type="submit" value="登录" />
        </div>
        }
    </div>
</body>
</html>

登录Model层方法代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication9.Models
{
    public class LoginDA
    {
        private newsDataContext _Context = new newsDataContext();
        public bool Login(string userName, string password)
        {
            var query = _Context.Login.Where(p => p.Username == userName && p.Password == password);
            return query.Count() > 0;
        }
    }
}
原文地址:https://www.cnblogs.com/dlexia/p/4649191.html