无废话版本-Asp.net MVC4.0 Rasor的基本用法

时间:2021-10-21 16:39:18

最近工作有点忙,好久没写东西了!废话不多说了,进入主题!

1.在页面中输出单一变量时候,只要在C#语句之前加上@符号即可,For example:

<p>Now Time:@DateTime.Now</p>

请注意,上述example中虽然使用C#语言撰写代码,但输出单一变量的时候,不需要加上分号;

2.在页面上输出一段含有空白字元或者运算子的结果时,必须在前后加上一个小括号,For example:

<p>
UserName:@(User.Identity.Name+Model.Member)
State :@(ViewBag.IsEnabled?"启用":"停用")
</p>

  --前台页面

@{
Layout = null;
}
@{
var htmlAttribute = ViewData["Dictionary"] as IDictionary<string, object>;
} <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Rasor</title>
</head>
<body>
<h1>Rasor基本语法</h1>
<div>
@using (Html.BeginForm("About", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.TextBox("Field", " ", new { type = "file", @class = "ed", size = "25" })<br />
@Html.Password("Pwd", "", htmlAttribute)<br />
@Html.TextBox("Email", "133030@qq.com", htmlAttribute)<br />
@Html.Hidden("Hidden", "Value", htmlAttribute)<br />
@Html.DropDownList("List",ViewData["List"] as SelectList,"请选择")
}
</div>
</body>
</html>

  

  后台方法:

public ActionResult Rasor()
{
IDictionary<string, object> attr = new Dictionary<string, object>();
attr.Add("size","32");
attr.Add("style","color:red");
ViewData["Dictionary"] = attr;
//DropdwonList的用法
List<SelectListItem> list = new List<SelectListItem>();
list.Add(new SelectListItem { Text="中国",Value="1"});
list.Add(new SelectListItem { Text = "美国", Value = "2" });
list.Add(new SelectListItem { Text = "俄国", Value = "3" });
ViewData["List"] = new SelectList(list, "Value", "Text", "");
return View();
}