MVC中Razor的使用 及路径问题
语法:
@ 可以编写一条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中
完!
MVC中Razor的使用 及路径问题的更多相关文章
- Spring MVC中获取当前项目的路径
Spring MVC中获取当前项目的路径 在web.xml中加入以下内容 <!--获取项目路径--> <context-param> <param-name>web ...
- MVC 中 Razor 无限分类的展示
在MVC的Razor视图展示无级分类的办法,在网上看了很多资料,大多搞得很高大上.可能本人水平有限,实在是不会用. 那我就用最简单爆力的办法来做. Model: public class NewsCa ...
- MVC中JQuery文件引入的路径问题,@Url.Content函数
今天写了个MVC的Demo,文件夹结构很简单,如下: 利用EF生成Model框架并手工加表字段注解,但在页面上JS验证始终没显示.实在无语. 无意中在浏览器里按F12,看见提示: Failed t ...
- 从项目经理的角度看.net的MVC中Razor语法真的很垃圾.
我们知道,Razor语法中我们可以直接使用@if(){}等代码段,这使得.net程序员在写模版时更容易了. 对比如下: 语法名称 Razor 语法 Web Forms 等效语法 代码块(服务端) @{ ...
- MVC中Razor视图基本语法(1)
Razor前面,必须要跟前面的有空隙,即空格(多谢一楼提醒,url里面确实不用空格,如果要在url里面只需要@(ViewBag.),加上括号就好了),之后的必须要连贯,否则加小括号 1,在页面中输出单 ...
- mvc中razor的一个bug
具体东西就不多说了,所有编译,代码都是木有问题的. 结果预览页面的时候竟然告诉我编译错误,尼玛这不科学啊. 来看看错误页面 看着问题大概应该是缺少} ,或者多了个} 倒置的编译错误才对,但是编译生成完 ...
- MVC 中 Razor引擎学习:RenderBody,RenderPage和RenderSection
RenderBody 在Razor引擎中没有了“母版页”,取而代之的是叫做“布局”的页面(_Layout.cshtml)放在了共享视图文件夹中.在这个页面中,会看到 标签里有这样一条语句: @Rend ...
- MVC中在RAZOR 模板里突然了现了 CANNOT RESOLVE SYMBOL ‘VIEWBAG’ 的错误提示
然后在Razor中出现了@ViewBag的不可用,@Url不可用,@Html 这些变量都不能用了. 异常提示: 编译器错误消息: CS0426: 类型“XX.Model.System”中不存在类型名称 ...
- ASP.NET MVC 3: Razor中的@:和语法
原文 ASP.NET MVC 3: Razor中的@:和语法 [原文发表地址] ASP.NET MVC 3: Razor’s @: and <text> syntax[原文发表时间] De ...
随机推荐
- 词法分析器Antlr
一.我们都知道编程语言在执行之前需要先进行编译,这样就可以把代码转换成机器识别的语言,这个过程就是编译. 那么它是怎么编译的呢? Java在JVM虚拟机中进行编译,javascript在Js引擎中编译 ...
- javascript对象的一点理解
<script type="text/javascript"> /* js对象:对象的职责是调用属性和调用方法 */ //1.对象的创建的三种方式 var obj = ...
- vpn分类[转]
目前常用的几种移动拨号的VPN技术及优势和劣势1) WEB SSL优点:1.使用简单:每个终端用户不需要安装客户端,使用起来方便,不需要维护终端用户,通过IE直接来访问. ...
- arrhelper::map
$array = [ ['id' => '123', 'name' => 'aaa', 'class' => 'x'], ['id' => '124', 'name' => ...
- JSTL标签使用说明
使用jstl需进行以下操作 a.下载jstl. b.解压jar文件将jstl.jar和standard.jar文件放到项目lib文件夹. c.在需要使用jstl地方引用标签库,比如在jsp页面引用以下 ...
- Codevs 1299 切水果
题目链接 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 简单的说,一共N个水果排成一排,切M次,每次切[L,R]区间的所有水果 ...
- 当select框变化时 获取select框中被选中的值
DOM <select name="course"> <option value="1">1</option> <op ...
- ant build utf-8
使用Ant编译过程中,报error: unmappable character for encoding UTF8 最简单的方法是在Build.xml文件中,在所有出现Javac的地方,增加一个选项: ...
- C#中判断字符是否大写
在C#中,通常判断一个字符是否为大写字母,有些人可能会第一时间想到用正则表达式,那除了正则表达式,是否还有其他方式呢? 答案是肯定的,先一睹为快,具体代码如下: using System; using ...
- android:layout_gravity 和 android:gravity 的区别
gravity 这个英文单词是重心的意思,在这里就表示停靠位置的意思. android:layout_gravity 和 android:gravity 的区别 从名字上可以看到,android:gr ...