语法:

@ 可以编写一条C#语句
@{} 可以编写一组C#语句
@: 将文字内容直接输出到页面上去
@() 在一句中将一段C#代码包括起来,证明这一句完整的C#代码

引用命名空间:@using 空间名称

Home控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication4.Models; namespace MvcApplication4.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/ public ActionResult Index()
{
return View();
}
public ActionResult insert()
{
return View();
} public ActionResult insert1(Users u)
{
new UsersData().insert(u);
return RedirectToAction("Index","Home");
}
public ActionResult insert2()
{
return View();
} }
}

Index视图层

@{
Layout = null;
}
@using MvcApplication4.Models;@*引用命名空间*@
<!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<table style="background-color:aqua;text-align:center;color:white; width:100%">
<tr style="background-color:orange;">
<td>姓名</td>
<td>密码</td>
<td>昵称</td>
<td>性别</td>
<td>生日</td>
<td>民族</td>
</tr>
@{
List<Users> ulist=new UsersData().all();
foreach(var u in ulist)
{
<tr style="@u.color">
<td>@u.UserName</td>
<td>@u.Password</td>
<td>@u.NickName</td>
<td>@(u.Sex.Value?"男":"女")</td>
<td>@u.Birthday.Value.ToString("yyyy-MM-dd")</td>
<td>@u.Nation1.NationName</td>
</tr>
}
} </table>
<a href="/home/insert">添加</a>
@Html.ActionLink("添加成员insert","insert","Home")
@Html.ActionLink("添加成员insert2","insert2","Home");
</div>
</body>
</html>

insert视图层

@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>insert</title>
</head>
<body>
@*不写form表单元素标签,用Razor来代替*@
<div>
<h1>这是添加界面insert</h1> @{using(Html.BeginForm("insert1","Home"))
{
@:用户名<input type="text" name="username" /><br /><br />
@:密码<input type="text" name="password" /><br /><br />
@:昵称<input type="text" name="nickname" /><br /><br />
@:性别<input type="text" name="sex" /><br /><br />
@:生日<input type="text" name="birthday" /><br /><br />
@:民族<input type="text" name="nation" /><br /><br />
<input type="submit" value="提交" />
}
}
@* 此处应该使用@Html.EndForm()结束,但是这样子会报错,所以用using来替代该功能*@
</div>
</body>
</html>

insert2视图层

@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>insert2</title>
</head>
<body>
<div>
<h1>这是添加界面insert2</h1> <form name="form1" method="post">
用户名<input type="text" name="username" /><br /><br />
密码<input type="text" name="password" /><br /><br />
昵称<input type="text" name="nickname" /><br /><br />
性别<input type="text" name="sex" /><br /><br />
生日<input type="text" name="birthday" /><br /><br />
民族<input type="text" name="nation" /><br /><br />
<input type="submit" id="tijiao" value="提交" /> </form> </div>
</body>
</html>
<script>
//在js中设置form表单的提交路径
document.getElementById("tijiao").onclick = function () { this.form.setAttribute("action","@Url.Action("insert1","Home")");
this.form.submit();
}; </script>

Users属性扩展

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace MvcApplication4.Models
{
public partial class Users
{
public string color
{
get
{
string end = "";
if (Convert.ToBoolean(this._Sex))
{
end = "background-color:gray;";
}
else
{
end = "background-color:red;";
} return end;
}
} }
}

Razor的路径

(1)Html.ActionLink("","","")     在Index视图层

(2)Html.BeginForm("","")      在insert视图层

(3)Url.Action("Insert1", "Home")    在insert2视图层的js中

完!

最新文章

  1. Oracle 增删改查
  2. jstl core 库 之 out set remove
  3. windows下用golang连接mssql
  4. Windows下tcp参数优化
  5. Axure一点
  6. Struts2的处理结果(三)——动态配置结果
  7. Swift - 自动布局库SnapKit的使用详解1(配置、使用方法、样例)
  8. win7 C# winForm编程 savefiledialog 不能弹出保存窗体
  9. 小学生之深入C#
  10. adb shell top
  11. WordPress创建过程
  12. python中json.loads,dumps,jsonify使用
  13. 015模块&mdash;&mdash;起别名
  14. CSS3背景,渐变
  15. redis学习(一)——redis介绍及安装
  16. vue 错误分析
  17. Codeforces Round #250 (Div. 1)E. The Child and Binary Tree
  18. 笔记react router 4(二)
  19. MySQL(一) 初识MySQL
  20. 安装部署Apache Hadoop (完全分布式模式并且实现NameNode HA和ResourceManager HA)

热门文章

  1. springMVC搭建
  2. 关于web-dev-server 记录
  3. 实时输出TextField中内容
  4. c#前3章总结
  5. Elasticsearch学习笔记(一)
  6. Java微信公众号开发
  7. Qt串口通信接收数据不完整的解决方法
  8. LeetCode Best Time to Buy and Sell Stock with Cooldown
  9. Python基础、 内置函数
  10. Vcenter server 5.5添加用户角色及分配权限