@using System.Text;
@model List<MvcShopping.Models.ProductCategory>
@{
ViewBag.Title = "测试程序";
}
<br />接收控制器传递的数据 @ViewBag.UserName
<br />显示Model的名称:@Html.DisplayNameFor(model => model[0])
<br />现在时间:@DateTime.Now
<br />启用状态:@ViewBag.Title
<br />多行C#代码
@{
var name = "小李";
var message = "你好,我是" + name;
<text> @*注释:不输入任何HTML标签*@
@message <br/> @*这是注释部分不显示*@
</text>
<span>我是小王</span><br/>
<text>
xiaoli@126.com
</text>
@ViewBag.Content; @*没有也不会报错*@
}
<br/>html内容和Razor混排:
@ViewBag.Title李琳<br/> @*直接当C#语法执行,不显示也不换行*@
@(ViewBag.Title)李小玲<br /> @*加括号后智能识别,且换行,*@
<span>@ViewBag.Title</span>李梅<br />
输出html标签元素
@{
var desc = "<span style='color:red'>这是HTML标签</span>";
@desc @*显示 <span style='color:red'>这是HTML标签</span>*@
<br/>
@Html.Raw(desc); @*显示红色字体“这是HTML标签”*@
}
@*条件判断语句*@
@if (ViewBag.Content == null)
{
<p>无此对象</p>
}
else
{
<p>有这个对象</p>
}
三元表达式:@(ViewBag.Content == null ? "无" : "有")
循环显示数据列表:
<ul>
@{
@*混合示例*@
<h3>成员列表</h3>
string[] peoples = { "will", "jame", "robin" };
foreach (var item in peoples)
{
<li>@item</li>
}
}
@foreach (var item in Model)
{
<li>@Html.DisplayNameFor(model => item.Name)</li>
<li>@Html.ActionLink(item.Name, "ProductList", new { id = item.Id })</li>
}
</ul>
辅助方法(可以新建类文件引用):
@helper ShowYesOrNo(int i)
{
if (i==0)
{
<text>否</text>
}
else
{
<text>是</text>
}
}
<p>
引用方法
@ShowYesOrNo(1)
</p>
自定义函数(可以新建类文件引用):
@functions
{
public static string ShowYN(int i)
{
var str=(i==0)?"否":"是";
return str;
}
}
<p>
引用方法
@ShowYN(1)
</p>
<p>
HTML辅助方法区
@Html.ActionLink("链接文字", "ActionName") <br />
@Html.ActionLink("链接文字", "ActionName", "ControllerName") <br />
@Html.ActionLink("链接文字", "ActionName", new { id = 123, page = 5 }) <br />
<a href="@Url.Action("ActionName")" class="linkButton">图片</a> <br />
@Html.RouteLink("回首页", new { type = "default" }) <br />
@Html.RouteLink("回首页", "Default", new { type = "index" }) --对应路由 <br />
@Html.RouteLink("回首页", new { type = "default" }, new { @class = "back" }) <br />
</p>
<p>
输出表单<br />
@using (Html.BeginForm("ActionName","ControllerName",FormMethod.Post))
{
<p>账号:@Html.TextBox("Account","张三")</p>
<p>密码:@Html.Password("pwd")</p>
@*添加自定义元素控制 可控制style,width,class等*@
<p>验证码:@Html.TextArea("code", "authcode", new { style = "width:100px;", rows = 10, @class = "taclass" })</p>
<input type="submit" value="登录"/>
}
</p>
<p>
输出表单上传文件<br />
@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<p>选择文件:@Html.TextBox("File1", "", new { type = "file", size = "25" })</p>
<input type="submit" value="上传文件" />
}
</p>
<p>
输出表单上传文件<br />
@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<p>选择文件:@Html.TextBox("File1", "", new { type = "file", size = "25" })</p>
<input type="submit" value="上传文件" />
}
</p>
<p>
模拟HTTP delete请求,符合REST接口请求标准<br />
@using (Html.BeginForm("DeleteMessage", "Message"))
{
@Html.HttpMethodOverride(HttpVerbs.Delete)
@Html.Hidden("id","123");
<input type="submit" value="删除此数据" />
}
</p>
MVC中View界面数据呈现示例
本文转载:CSDN博客