MVC 【ASPX视图引擎】
新建项目----ASP.NET MVC 4 Web 应用程序------选择模板(空)、视图引擎(ASPX)
1、认识控制器Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC1.Models; namespace MVC1.Controllers //命名空间
{
//控制器名称 //继承Controller
public class HomeController : Controller
{ //action 动作 每一个动作决定你要干什么(方法) ActionResult返回房类型
public string Index()
{
return "你好!世界";
} }
}
用到模型数据时需要引用命名空间
using 项目名.Models
2、认识模型Model
LinQ 放在model里面,
3、认识视图 View
在返回视图的动作中右键添加视图
只有在 控制器中 ActionResult 类型的动作中才可以有视图
public ActionResult Index()
{
return View();
}
用到模型数据时需要引用命名空间(最上方)
<% @ Import Namespace ="mvc2.Models" %>
引入 命名空间 项目名.模板名
案例分析:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace mvc2.Models
{
public class UsersData
{
DataClasses1DataContext con = new DataClasses1DataContext();
public List<Users> SelectAll()
{
return con.Users.ToList();
} public bool Insert(Users u)
{
bool ok = false;
try
{
con.Users.InsertOnSubmit(u);
con.SubmitChanges();
ok = true;
}
catch { } return ok;
} public bool DeleteUser(string ids)
{
bool ok = false; Users u = con.Users.Where(r => r.Ids.ToString() == ids).FirstOrDefault();
if (u != null)
{
con.Users.DeleteOnSubmit(u);
con.SubmitChanges();
ok = true;
} return ok;
} public Users SelectUser(string ids)
{
return con.Users.Where(r => r.Ids.ToString() == ids).FirstOrDefault();
} }
}
UsersData
using mvc2.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace mvc2.Controllers
{
public class HomeController : Controller
{
//展示
public ActionResult Index()
{
return View();
} //添加视图
public ActionResult Insert()
{
return View();
} //添加计算
public ActionResult Insert1(string username, string password, string nickname, string sex, string birthday, string nation)
{
Users u = new Users();
u.UserName = username;
u.PassWord = password;
u.NickName = nickname;
u.Sex = Convert.ToBoolean(sex);
u.Birthday = Convert.ToDateTime(birthday);
u.Nation = nation; bool ok = new UsersData().Insert(u);
Session["InsertOK"] = ok; return RedirectToAction("Index", "Home");
//重定向 ( 动作名 , 控制器名 )
} //删除
public ActionResult Delete(string id)
{
bool ok = new UsersData().DeleteUser(id); return RedirectToAction("Index");
} //修改
public ActionResult Update(string id)
{
Users u = new UsersData().SelectUser(id); //根据id查询出u ViewBag.hehe = u; //试图数据包,传递数据 return View();
} }
}
控制器
--删除,不需要视图展示,直接在动作上 计算
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <%@ Import Namespace="mvc2.Models" %> <!DOCTYPE html> <html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<% if (Convert.ToBoolean(Session["InsertOK"]) == true)
{%> <script type="text/javascript">
alert('添加成功!'); </script>
<%
Session["InsertOK"] = null;
} %> <table style="width: 100%; text-align: center; background-color: navy;">
<tr style="color: white;">
<td>用户名</td>
<td>密码</td>
<td>昵称</td>
<td>性别</td>
<td>生日</td>
<td>民族</td>
<td>操作</td>
</tr>
<%
List<Users> ulist = new UsersData().SelectAll(); foreach (Users u in ulist)
{
%>
<tr style="background-color: white;">
<td><%=u.UserName %></td>
<td><%=u.PassWord %></td>
<td><%=u.NickName %>同学</td>
<td><%=u.Sex.Value?"男":"女" %></td> <%--.value 数据转换--%>
<td><%=u.Birthday.Value.ToString("yyyy年MM月dd日") %></td>
<td><%=u.UserNation.NationName %></td>
<td>
<a href="Home/Update/<%=u.Ids %>">修改</a> |
<a href="Home/Delete/<%=u.Ids %>">删除</a> </td>
</tr>
<%
}
%>
</table>
<input type="button" value="添加" onclick="window.open('Home/Insert', '_blank');" /> </div>
</body>
</html>
主展示视图
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <!DOCTYPE html> <html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>Insert</title>
</head>
<body>
<div>
<h1>添加用户</h1> <form action="Insert1" method="post"> <%--表单--%>
<%-- 提交到哪 提交方式--%> 用户名:
<input type="text" name="username" /><br />
密码:
<input type="text" name="password" /><br />
昵称:
<input type="text" name="nickname" /><br />
性别:
<input type="text" name="sex" /><br />
生日:
<input type="text" name="birthday" /><br />
民族:
<input type="text" name="nation" /><br />
<input type="submit" value="保存" /> </form> </div>
</body>
</html>
添加——用表单提交
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <%@ Import Namespace="mvc2.Models" %> <!DOCTYPE html> <html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>Update</title>
</head>
<body> <h1>修改用户</h1> <% Users u = ViewBag.hehe; %>
<%-------------将传进来的数据取出--%> <form action="Update1" method="post">
用户名: <%-----------绑定数据--%>
<input type="text" name="username" value="<%=u.UserName %>" /><br />
密码:
<input type="text" name="password" value="<%=u.PassWord %>" /><br />
昵称:
<input type="text" name="nickname" value="<%=u.NickName %>" /><br />
性别:
<input type="text" name="sex" value="<%=u.Sex %>" /><br />
生日:
<input type="text" name="birthday" value="<%=u.Birthday %>" /><br />
民族:
<input type="text" name="nation" value="<%=u.Nation %>" /><br />
<input type="submit" value="保存" /> </form> </body>
</html>
修改——表单提交
ViewBag 传值
ViewBag.变量=值
ViewBag.a=u; -- 动作中传
<% Users u = ViewBag.a %> -- 视图中接收
可以传任何类型的值,只能在本视图中传值,
MVC 【ASPX视图引擎】的更多相关文章
- 2014-07-29 浅谈MVC框架中Razor与ASPX视图引擎
今天是在吾索实习的第15天.随着准备工作的完善,我们小组将逐步开始手机端BBS的开发,而且我们将计划使用MVC框架进行该系统的开发.虽然我们对MVC框架并不是非常熟悉,或许这会降低我们开发该系统的效率 ...
- ASP.NET MVC——Razor视图引擎
Razor是MVC框架视图引擎,我们今天就来说一说Razor视图引擎. 首先还是来创建一个基础项目叫Razor来演示. 先来定义一个Model叫Product public class Product ...
- 第5章——使用 Razor(MVC框架视图引擎)
Razor 是MVC框架视图引擎的名称. 本章提供 Razor 语法的快速教程,以使你能够识别 Razor 表达式. 本章不打算提供 Razor 的完整参考,而将其视为一个语法速成教程.在本书的后续内 ...
- MVC ViewEngine视图引擎解读及autofac的IOC运用实践
MVC 三大特色 Model.View.Control ,这次咱们讲视图引擎ViewEngine 1.首先看看IViewEngine接口的定义 namespace System.Web.Mvc { ...
- ASP.NET MVC 对于视图引擎的优化
我经常使用asp.net MVC框架来做网站.总的来说,MVC框架是一个非常优秀的框架.相对于曾经的web form模式,我个人感觉分工更加合理思路也更加清晰,但是交给开发人员的工作也相对变多了. 当 ...
- ASP.NET MVC Razor视图引擎攻略
--引子 看下面一段MVC 2.0的代码. <%if (Model != null){%> <p><%=Model%></p><%}%>&l ...
- ASP.NET MVC自定义视图引擎ViewEngine 创建Model的专属视图
MVC内置的视图引擎有WebForm view engine和Razor view engine,当然也可以自定义视图引擎ViewEngine. 本文想针对某个Model,自定义该Model的专属视图 ...
- ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 视图模板页
https://www.cnblogs.com/xlhblogs/archive/2013/06/09/3129449.html MVC Razor模板引擎 @RenderBody.@RenderPa ...
- ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 HtmlHelper-超链接方法
一.@Html.ActionLink()概述 在MVC的Rasor视图引擎中,微软采用一种全新的方式来表示从前的超链接方式,它代替了从前的繁杂的超链接标签,让代码看起来更加简洁.通过浏览器依然会解析成 ...
随机推荐
- ABP框架系列之三十二:(Logging-登录)
Server Side(服务端) ASP.NET Boilerplate uses Castle Windsor's logging facility. It can work with differ ...
- codeforces 508B
B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- JDBC几种常见的数据库连接
JDBC的URL=协议名+子协议名+数据源名.a 协议名总是“jdbc”.b 子协议名由JDBC驱动程序的编写者决定.c 数据源名也可能包含用户与口令等信息:这些信息也可单独提供.几种常见的数据库连接 ...
- 在Azure DevOps Server的代理服务器安装Python环境
Python和Azure DevOps Server Python是一种计算机程序设计语言.是一种动态的.面向对象的脚本语言,最初主要为系统运维人员编写自动化脚本,在实际应用中,Python已经在前端 ...
- Markdown新手教程
目录 什么是Markdown? 用Markdown写作有什么优缺点? 有哪些比较好的Markdown写作工具? markdown语法 标题 水平分区线 引用 中划线 斜体 粗体 斜粗体 链接 图片 无 ...
- 第30节:Java基础-内部类
内部类 // 外部类 class Demo{ private int num = 3; // 定义内部类 class Int{ void show(){ System.out.println(&quo ...
- JS应用实例3:定时弹出广告
在观看视频时候总会发现有广告弹出 这里就做一个类似这样的定时弹出广告的实例: 前面的JS代码和HTML写在同一个文件,实际开发中总是分开来写 用的时候引入即可 HTML代码: <!DOCTYPE ...
- 零碎的java知识点记录(一)
小知识点 Map有getOrDefault("1","0");取不到取默认值 两个不同对象,属性相同进行赋值转换,使用modelMapper <depen ...
- Python Web Service
搞移动端有段时间了,一直使用别人的API,自己也只写过ASP.NET网站作为网络服务,相对来讲是很大的短板.虽然ASP.NET可以提供想要的web服务,但是其体量臃肿,响应速度非常慢,这点我非常不喜欢 ...
- LeetCode--No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...