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 ... 
随机推荐
- ssh框架开发问题
			Struts + spring MVC + hibernate 6.1 从职责上分为表示层.业务逻辑层.数据持久层和域模块层四层. 其中使用Struts作为系统的整体基础架构,负责MVC的分离 ... 
- Print all nodes at distance k from a given node
			Given a binary tree, a target node in the binary tree, and an integer value k, print all the nodes t ... 
- 使用PowerShell向SharePoint中写入数据
			本文介绍了如何在命令行方式下, 创建自定义列表, 将外部数据导入到列表以及生成视图. $listname = "contact0422" $column_text = @( &qu ... 
- HTTP常见错误代码总结
			1.HTTP 401 用户验证失败.不允许继续访问 2.HTTP 403 禁止访问,访问web应用,没有指定要访问页面的名称 3.HTTP 404 请求的文件找不到,一般情况是在浏览器输入地址时,输入 ... 
- 点击某个按钮弹出 photoswip
			var openPhotoSwipe = function() { var pswpElement = document.querySelectorAll('.pswp')[0]; // build ... 
- what's the difference between dim as and dim as new?
			what's the difference between dim as and dim as new? There is no difference with value types (Intege ... 
- ExtJS笔记   Tree
			The Tree Panel Component is one of the most versatile Components in Ext JS and is an excellent tool ... 
- sublime问题汇总
			1.sunlime光标不跟随 解决方法:安装插件IMESupport 利用package control安装,步骤: 第一步 第二步 
- Android MP3录音实现
			给APP做语音功能,必须考虑到IOS和Android平台的通用性.wav录音质量高,文件太大,AAC和AMR格式在IOS平台却不支持,所以采用libmp3lame把AudioRecord音频流直接转换 ... 
- Mongo使用脚本更新数据
			SQL Server中我们经常要使用脚本来刷一些数据,在mongo中我们也可以使用mongo的脚本来刷mongo的数据 首先在命令窗口中链接到本地的mongo库 load("[脚本的地址]& ... 
