开发者

asp.net cshtml can't run asp.net code

I'm following the tutorial at asp.net mvc3 at 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>

the 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);
        }
    }
}

But it seems that in cshtml, it can't run the asp.net code, only display the static content, why?

And the cshtml also don't apply to the MainContent master page, t开发者_高级运维here is only plain text without any background and button when I run the application, where's the problems?


To have your for to work, you must set the value of ViewBag in the controller.

Change the the controller like this

public ActionResult Welcome(string name, int numTimes = 1)
{
    ViewBag.Message = name;
    ViewBag.NumTimes = numTimes;
    return View();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜