您的位置:首页 > 其它

MVC Razor 登录

2015-07-09 21:33 302 查看
@using MvcApplication1.Models;

@{
Layout = null;
}

<!DOCTYPE html>

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




LogInBF

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

namespace MvcApplication1.Models
{
public class LogInBF
{
private NewsDBDataContext Context = new NewsDBDataContext();
public bool LogInCheck(string uid,string pwd)
{
var query = Context.users.Where(p=>p.uids==uid && p.pwd==pwd);
if (query.Count()>0)
{
return true;
}
return false;
}
}
}


LogInController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class LogInController : Controller
{
//
// GET: /LogIn/

public ActionResult Index()
{

return View();
}

public ActionResult panduai(string uid, string pwd)
{
bool isok = new LogInBF().LogInCheck(uid,pwd);
if (isok)
{
Session["user"] = uid;//保存登录状态,添加会话保存当前的登录账户
return RedirectToAction("Index", "Home");
}
else
{
return RedirectToAction("Index");
}
}
}
}


HomeController

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

namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/

//查询所有信息
public ActionResult Index()
{
//调取会话中信息,判断是否为空,不为空证明是从登录界面登录过来的,是空的话证明没有登录
if (Session["user"]==null)
{
return RedirectToAction("Index","LogIn");
}
List<News> s = new NewsBF().Select();
return View(s);
}

public ActionResult xiugai(string id)
{
if (Session["user"] == null)
{
return RedirectToAction("Index", "LogIn");
}
News s= new NewsBF().Select(id);
return View(s);
}

public ActionResult tianjia()
{
if (Session["user"] == null)
{
return RedirectToAction("Index", "LogIn");
}
News s = new News();//传递一个空的对象,给对象赋满值后添加进数据库
return View(s);
}

public ActionResult Insert(News s)
{
if (Session["user"] == null)
{
return RedirectToAction("Index", "LogIn");
}
new NewsBF().Insert(s);
return RedirectToAction("Index");
}

public ActionResult Update(News s)
{
if (Session["user"] == null)
{
return RedirectToAction("Index", "LogIn");
}
new NewsBF().Update(s);
return RedirectToAction("Index");
}

public ActionResult Delete(string id)
{
if (Session["user"] == null)
{
return RedirectToAction("Index", "LogIn");
}
try
{
new NewsBF().Delete(id);
return RedirectToAction("Index");
}
catch (Exception)
{

throw;
}
}

}
}


NewsBF

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

namespace MvcApplication1.Models
{
public class NewsBF
{
private NewsDBDataContext Context = new NewsDBDataContext();

//插入
public void Insert(News s)
{

s.times = DateTime.Now;

Context.News.InsertOnSubmit(s);
Context.SubmitChanges();

}

//按ID查询
public News Select(string id)
{
var query = Context.News.Where(p=>p.newsid==int.Parse(id));
if (query.Count()>0)
{
News s = query.First();
return s;
}
return null;
}

//删除
public void Delete(string id)
{
var query = Context.News.Where(p=>p.newsid==int.Parse(id));
if (query.Count()>0)
{
News s = query.First();
Context.News.DeleteOnSubmit(s);
Context.SubmitChanges();
}
}

//修改
public void Update(News n)
{
var query = Context.News.Where(p=>p.newsid==n.newsid);
if (query.Count()>0)
{
News s = query.First();
s.newsid = n.newsid;
s.title = n.title;
s.zname = n.zname;
s.laiyuan = n.laiyuan;
s.txt = n.txt;
s.times = n.times;
Context.SubmitChanges();
}
}

//查询全部信息
public List<News> Select()
{
return Context.News.ToList();
}
}
}


Index.cshtml

@using MvcApplication1.Models;
@model List<News>
@{
Layout = null;
}

<!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;
}
.tbrow1 {
text-align:center;
background-color:#FFFFcc;
}
#f1 {
margin-left:600px;
margin-top:0px;
}
</style>
</head>
<body>
<div>
<table id="tbstu" width="100%" cellpadding="4" cellspacing="1" border="0">
<tr class="tbhead">
<td>编号</td>
<td>标题</td>
<td>作者</td>
<td>来源</td>
<td>内容</td>
<td>日期</td>
<td>更新</td>
<td>操作</td>
</tr>
@{
foreach(News s in Model)
{
<tr class="tbrow1">
<td>@s.newsid</td>
<td>@s.title</td>
<td>@s.zname</td>
<td>@s.laiyuan</td>
<td>@s.txt</td>
<td>@s.times</td>
<td>
@Html.ActionLink("修改", "xiugai", "Home", new { id=s.newsid},null)
</td>
<td>
@Html.ActionLink("删除", "Delete", "Home", new { id = s.newsid }, new {onclick="return confirm('确认删除 "+@s.title+" 吗?')" })
</td>
</tr>
}
}
</table>
<form id="f1" action="/Home/tianjia">
<input type="submit" value="添加" />
</form>
</div>
</body>
</html>




tianjia.cshtml

@using MvcApplication1.Models;
@model News
@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>tianjia</title>
<style type="text/css">
.tbhead {
background-color: navy;
text-align: center;
color: white;
font-weight: bold;
}
.tbrow1 {
text-align:center;
background-color:#FFFFcc;
}
#f1 {
margin-left:600px;
margin-top:-26px;
}
</style>
</head>
<body>
<div>
@using (Html.BeginForm("Insert", "Home", FormMethod.Post))
{
<table id="tbstu" width="100%" cellpadding="4" cellspacing="1" border="0">
<tr class="tbhead">
<td colspan="3">发布新闻
</td>
</tr>
<tr style="display:none">
<td>
@Html.HiddenFor(p=>p.newsid)
@Html.HiddenFor(p=>p.times)
</td>
</tr>
<tr class="tbrow1">
<td width="20%"></td>
<td>标题:</td>
<td>@Html.TextBoxFor(p=>p.title)</td>
</tr>
<tr class="tbrow1">
<td width="20%"></td>
<td>作者:</td>
<td>@Html.TextBoxFor(p=>p.zname)</td>
</tr>
<tr class="tbrow1">
<td width="20%"></td>
<td>来源:</td>
<td>@Html.TextBoxFor(p=>p.laiyuan)</td>
</tr>
<tr class="tbrow1">
<td width="20%"></td>
<td>内容:</td>
<td height="200px">@Html.TextAreaFor(P => P.txt, new {rows="10" })</td>
</tr>
<tr class="tbrow1">
<td colspan="3">

<input type="submit" value="提交" />

</td>
</tr>
</table>
}
<form id="f1" action="/Home/Index" method="post">
<input type="submit" value="查看" />
</form>
</div>
</body>
</html>




xiugai.cshtml

@using MvcApplication1.Models;
@model News
@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>xiugai</title>
<style type="text/css">
.tbhead {
background-color: navy;
text-align: center;
color: white;
font-weight: bold;
}
.tbrow1 {
text-align:center;
background-color:#FFFFcc;
}
#f1 {
margin-left:600px;
margin-top:-26px;
}
</style>
</head>
<body>
<div>
@using (Html.BeginForm("Update", "Home", FormMethod.Post))
{
<table id="tbstu" width="100%" cellpadding="4" cellspacing="1" border="0">
<tr class="tbhead">
<td colspan="3">发布新闻
</td>
</tr>
<tr class="tbrow1" style="display:none">
<td width="20%"></td>
<td>标号:</td>
<td>@Html.TextBoxFor(p => p.newsid)</td>
</tr>
<tr class="tbrow1" style="display:none">
<td width="20%"></td>
<td>标号:</td>
<td>@Html.TextBoxFor(p => p.times)</td>
</tr>
<tr class="tbrow1">
<td width="20%"></td>
<td>标题:</td>
<td>@Html.TextBoxFor(p=>p.title)</td>
</tr>
<tr class="tbrow1">
<td width="20%"></td>
<td>作者:</td>
<td>@Html.TextBoxFor(p=>p.zname)</td>
</tr>
<tr class="tbrow1">
<td width="20%"></td>
<td>来源:</td>
<td>@Html.TextBoxFor(p=>p.laiyuan)</td>
</tr>
<tr class="tbrow1">
<td width="20%"></td>
<td>内容:</td>
<td height="200px">@Html.TextAreaFor(P=>P.txt,new {rows="10",cols="30" })</td>
</tr>
<tr class="tbrow1">
<td colspan="3">

<input type="submit" value="修改" />

</td>
</tr>
</table>
}
<form id="f1" action="/Home/Index" method="post">
<input type="submit" value="查看" />
</form>
</div>
</body>
</html>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: