我正在关注 asp.net mvc3 上的教程 http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part3-cs
@{
ViewBag.Title = "Welcome";
}
<h2>Welcome</h2>
<ul>
@for (int i=0; i < ViewBag.NumTimes; i++) {
<li>@ViewBag.Message</li>
}
</ul>
hellowWorldcontroller
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
public class HelloWorldController : Controller
{
// GET: /HelloWorld/
/*public void Index()
{
//return "This is my <b>default</b> action...";
return View;
}*/
public ActionResult Index()
{
//ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
//
// GET: /HelloWorld/Welcome/
public ActionResult Welcome(string name, int numTimes = 1)
{
return View();
//return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes);
}
}
}
但似乎在cshtml
中,它无法运行asp.net
代码,只显示静态内容,为什么?
并且cshtml
也不适用于MainContent
母版页,在运行应用程序时,只有纯文本没有任何背景和按钮,问题出在哪里?
答案 0 :(得分:1)
要让for
正常工作,您必须在控制器中设置ViewBag的值。
像这样更改控制器
public ActionResult Welcome(string name, int numTimes = 1)
{
ViewBag.Message = name;
ViewBag.NumTimes = numTimes;
return View();
}