0

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, there is only plain text without any background and button when I run the application, where's the problems?

4
  • Because you need to set ViewBag.NumTimes = numTimes; in Welcome action. Commented Sep 30, 2011 at 3:48
  • ViewBag.Title = "Welcome"; //no effect, why? Commented Sep 30, 2011 at 3:50
  • @hkinterview, for viewbag title to take effect, you need to use it in the markup - for example, <title>@ViewBag.Title</title> in html head section. I guess that it must be located in some layout page. Commented Sep 30, 2011 at 4:24
  • @VinayC thanks, i think the tutorial is wrong. Commented Sep 30, 2011 at 5:23

1 Answer 1

1

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();
}
Sign up to request clarification or add additional context in comments.

6 Comments

how about the master page problem?
If you mean the title, it's the same, set ViewBag.Title = "hello". you will find the call in Shared/_layout.cshtml. If you mean the css, in layout check for the correct path of scripts and css
you see this asp.net/mvc/tutorials/getting-started-with-mvc3-part3-cs, the content display in white area, but my application, it only displays the Welcome.cshtml
It should work if you followed the step in the tutorial (and it seems so), maybe your project is broken. Have you tried to make a new MVC3 project from scratch? Maybe you changed something by mistake in the _layout.cshtml.
i can't find _layout.cshtml, where is it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.