MVC4学习笔记(一)
1.查询
1)Controllers
/// <summary>
/// 数据上下文对象
/// </summary>
OumindBlogEntities db = new OumindBlogEntities(); #region 查询文章列表
/// <summary>
/// 查询文章列表
/// </summary>
/// <returns></returns>
public ActionResult Index()
{ //linq
List<Models.BlogArticle> list = (from d in db.BlogArticles where d.AIsDel == false select d).ToList();
//1.2将集合数据传给视图
ViewBag.DataList = list;
//ViewData["DataList"] = list;
return View();
} #endregion
2)View
@using MVCBlog.Models
<!--引入命名空间-->
<table id="tbList">
<tr>
<th>id</th>
<th>标题</th>
<th>分类</th>
<th>状态</th>
<th>时间</th>
<th>操作</th>
</tr>
@foreach (BlogArticle a in ViewBag.DataList as List<BlogArticle>)
{
<tr>
<td>@a.AId</td>
<td>@a.ATitle</td>
<td>@a.BlogArticleCate.Name</td>
<td>@a.Enumeration.e_cname</td>
<td>@a.AAddtime</td>
<td>
<a href="javascript:del(@a.AId)">删除</a>
<a href="/Home/Modify/@a.AId">修改</a>
</td>
</tr> }
</table>
2.删除
1)Controllers
#region 删除操作
/// <summary>
/// 删除操作
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Del(int id)
{
try
{
//1.创建要删除的对象
BlogArticle modelDel = new BlogArticle { AId = id };
//2.将对象添加到 EF 管理容器
db.BlogArticles.Attach(modelDel);
//3.将对象包装类的状态 标识为 删除状态
db.BlogArticles.Remove(modelDel);
//4.更新到数据库
db.SaveChanges();
//5.更新成功,则命令浏览器 重定向到 /Home/List 方法
return RedirectToAction("Index", "Home");
}
catch (Exception ex)
{
return Content("删除失败~~~" + ex.Message);
} }
#endregion
3.修改
1)Controller
using System.Data.Entity.Infrastructure;
#region 显示要修改的数据
/// <summary>
/// 显示要修改的数据
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Modify(int id)
{
BlogArticle model = (from a in db.BlogArticles where a.AId == id select a).FirstOrDefault(); //生成文章分类 下拉框 列表集合 <option value="1">文本</option>
IEnumerable<SelectListItem> listItem = (from c in db.BlogArticleCates
where c.IsDel == false
select c).ToList().Select(c => new SelectListItem { Value=c.Id.ToString(),Text=c.Name});
//将生成的文章分类 下来框 列表集合 设置给ViewBag
ViewBag.CateList = listItem; //使用View 构造函数,将实体传递给视图上名为Model的属性
return View(model); }
#endregion #region 0.5 执行修改
[HttpPost]
/// <summary>
/// 0.5 执行修改
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public ActionResult Modify(BlogArticle model)
{
//1.将实体对象a 加入到EF 对象容器中,并 b获取 伪包装类 对象
DbEntityEntry<BlogArticle> entry = db.Entry<BlogArticle>(model);
//2.将包装类对象 的状态设置为 Unchanged
entry.State = System.Data.EntityState.Unchanged;
//3.设置 被改变的 属性
entry.Property(a => a.ATitle).IsModified = true;
entry.Property(a => a.AContent).IsModified = true;
entry.Property(a => a.ACate).IsModified = true;
//4.提交到数据库,完成修改
db.SaveChanges();
//5.命令 浏览器 重定向 到 Home/List 方法
return RedirectToAction("Index", "Home"); }
#endregion
2)View
@model MVCBlog.Models.BlogArticle
<!--指定页面Model 属性 的类型-->
@using(Html.BeginForm("Modify","Home",FormMethod.Post))
{ <table id="tbList">
<tr><td colspan="2">修改:@Html.HiddenFor(a=>a.AId)</td></tr>
<tr>
<td>标题:</td>
<td>@Html.TextBoxFor(a=>a.ATitle)</td>
</tr>
<tr>
<td>分类:</td>
<!--使用强类型方法生成下拉框,并自动根据 model属性里的ACate值 设置 下拉框的默认选中项-->
<td>@Html.DropDownListFor(a => a.ACate, ViewBag.CateList as IEnumerable<SelectListItem>)</td>
</tr>
<tr>
<td>内容:</td>
<td>@Html.TextAreaFor(a=>a.AContent,10,60,null)</td>
</tr>
<tr>
<td colspan="2" ><input type="submit" value="确认修改"/>@Html.ActionLink("返回","Index","Home")</td>
</tr>
</table>
}
MVC4学习笔记(一)的更多相关文章
- MVC4学习笔记之--身份认证过滤器
过滤器作为MVC模式中面向切面编程应用很广泛,例如身份验证,日志,异常,行为截取等.博客园里面的大神对应过滤器的介绍以及很多,MVC4中不同的过滤器也介绍得很清楚.FlyDragon 辉太 禁止吸烟 ...
- asp.net mvc4 学习笔记一(基本原理)
做了8年的asp.net webform,用过MVVM但还没用过MVC , 虽然项目不用MVC,但是还是想了解一下,今天第二天学习,以下是学习心得. VS2012默认带有asp.net mvc3和as ...
- ASP.NET MVC4学习笔记路由系统概念与应用篇
一.概念 1.路由是计算机网络中的一个技术概念,表示把数据包从一个网段转发至另一网段.ASP.NET中的路由系统作用类似,其作用是把请求Url映射到相应的"资源"上,资源可以是一段 ...
- ASP.NET MVC4学习笔记之总体概述
断断续续使用ASP.NET MVC框架也有一年多了,也算积累了一些经验,唉,一直想写一些笔记好好总结一下,人太懒不想动笔,今天终于决定开始.希望自己能坚持下去. 这篇文章大体介绍ASP.NET MVC ...
- MVC4 学习笔记01
1 . ASP.NET MVC 中 ActionResult 和 ViewResult 在使用上的区别是什么?要注意什么吗? ActionResult 是一个抽象(abstract)类,ViewRes ...
- ASP.NET MVC4学习笔记
一.MVC简介
- Mvc4学习笔记一(Ajax.ActionLink)
<style type="text/css"> #left {width:200px; min-height:500px;border:1px solid #ccc;f ...
- MVC4 学习笔记 之 URL中存在编译的空格 20%20%
/Config/Edit/QQCC%20%20%20%20%20%20%20 原因是: 通过EF直接添加了空格? NO 是因为你的数据库字段设计问题,因为你当然设计如>:sID nchar(10 ...
- VS2012 MVC4 学习笔记-概览
1. 访问请求过程 访问收到后路由(Router)根据路径由分配给对应的控制器(Control),然后由控制器返回页面视图(View) 路由设置一个默认的控制器,类似 主页的样子吧 <未完待续& ...
随机推荐
- 类似区间计数的种类并查集两题--HDU 3038 & POJ 1733
1.POJ 1733 Parity game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5744 Accepted: ...
- SQL Server进制
在项目中,大家可能都遇到过,需要把十进制转换为其他进制的情况,google上一搜,已经有很多2进制.8进制.16进制和十进制的转换方法.但是在一些项目中,这些可能无法满足要求,可能需要17.18甚至是 ...
- Android Studio系列教程一--下载和安装
原文链接:http://stormzhang.com/devtools/2014/11/25/android-studio-tutorial1/ 背景 相信大家对Android Studio已经不陌生 ...
- 16进制色值转换成RGB
#51147f 转换成RGB ,5*16+1 ,1*16+4,7*16+15 #A9A9A9 转换成RGB ,A*16+9 ,A*16+9,A*16+9
- localStorage实现购物车数量单价和结算页面的实时同步
While there is life there is hope.一息若存,希望不灭 用localStorage实现简易的购物车数量单价和结算页面两个页面的实时同步: 购物车页面:实时更新页面,在i ...
- 关闭log4j 输出 DEBUG org.apache.commons.beanutils.*
2016-03-23 10:52:26,860 DEBUG org.apache.commons.beanutils.MethodUtils - Matching name=getEPort on c ...
- JsonHelper
.net下的json序列化在以前没有Newtonsoft.Json崭露头角之前采用System.Web.Script.Serialization命名空间下的JavaScriptSerializer对象 ...
- NET中MSMQ的使用----附例子
目录 一:MSMQ的一些理论上的知识 二:队列类型(Queue Type) 三:安装消息队列 四:在C#中Messagequeue class 五:MSMQ-发送消息到远程专用队列 六:例子 一. ...
- LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- linux内核分析 课程总结
Linux内核分析 链接汇总 Linux内核分析第一周学习总结--计算机是如何工作的 Linux内核分析第二周学习总结--操作系统是如何工作的 Linux内核分析第三周学习总结--构造一个简单的Lin ...