Razor的使用
控制器里的内容
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace mvc.Controllers.home { public class homeController : Controller { // // GET: /home/ public ActionResult Index() { ViewBag.massage = "hello word"; return View(); } } }
视图里读取控制器返回的内容和调用c#对象
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title></title> </head> <body> <!--页面内--> @{var abc = "这是标题";} <div><p>@abc</p> <!--这里是控制器输出--> @ViewData["massage"] <br /><br /> <!--引用对象--> @DateTime.Now.Hour 时 @DateTime.Now.Minute 分 @DateTime.Now.Second 秒 @{ var txt = ""; if (DateTime.Now.Hour > 12) { txt = "Good Evening"; } else { txt = "Good Morning"; } } <p>@txt</p> <!--接收用户输入的值--> <p>计算器功能</p> @{ var totalMessage = ""; if (IsPost) { var num1 = Request["text1"]; var num2 = Request["text2"]; var total = num1.AsInt() + num2.AsInt(); totalMessage = "Total = " + total; } } <form action="" method="post"> <p> <label for="text1">First Number:</label><br> <input type="text" name="text1" /> </p> <p> <label for="text2">Second Number:</label><br> <input type="text" name="text2" /> </p> <p><input type="submit" value=" Add " /></p> </form> <p>@totalMessage</p> </div> </body> </html>
时间就像海绵里的水,只要你愿意挤,总还是有的——鲁迅