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视图引擎中,微软采用一种全新的方式来表示从前的超链接方式,它代替了从前的繁杂的超链接标签,让代码看起来更加简洁.通过浏览器依然会解析成 ...
随机推荐
- mui 页面提示:Unable to preventDefault inside passive
页面提示: 点击该事件:页面提示:[8mui.min.js:7 [Intervention] Unable to preventDefault inside passive event listene ...
- 前端基于easyui的mvc扩展
背景 由于MVC的前端是基于jquery.validate和jquery.validate.unobtrusive来实现的,但是当我们要使用其他的ui组件且组件本身就带有完整的验证功能的话,那么要让它 ...
- python 使用unittest进行单元测试
import unittest import HTMLTestRunner """ Python中有一个自带的单元测试框架是unittest模块,用它来做单元测试,它里面 ...
- 小程序基础知识点讲解-WXML + WXSS + JS,生命周期
小程序基础 小程序官方地址,小程序开发者工具,点击此处下载.在微信小程序中有一个配置文件project.config.json,此文件可以让开发者在不同设备中进行开发. 微信小程序共支持5种文件,wx ...
- 如何使用react-redux——傻瓜版
概述 之前看redux官方文档真是看得一脸懵逼,现在自认为会用了,于是来总结一下用法,供以后开发时参考,相信对其他人也有用. 不得不说,用了redux之后感觉挺爽的,有如下优点: 组件大多是函数组件非 ...
- Typescript 学习笔记四:回忆ES5 中的类
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- 使用Swagger 搭建高可读性ASP.Net WebApi文档
一.前言 在最近一个商城项目中,使用WebApi搭建API项目.但开发过程中,前后端工程师对于沟通接口的使用,是非常耗时的.之前也有用过Swagger构建WebApi文档,但是API文档的可读性并不高 ...
- python之函数参数问题(参数为可变对象)
今天看到一段代码,其中函数入参有一个参数为list,类似如下: def linux_monitor(pid=0,pidlist = []): pidlist.append(pid) 通过测试发现是有问 ...
- laravel框架的安装与配置
正常安装: 1.composer.(https://getcomposer.org/Composer-Setup.exe) 安装之前要确保目录:wamp\bin\php\php5.4.3下的php.i ...
- 安尼泰科T1行车记录仪说明书
点击下载:安尼泰科T1行车记录仪说明书 自己总结:行车记录仪_使用总结.rar PS:我的型号是T1C,但说明书也适合.