Okay, so I've got this code:
public ActionResult Welcome(string name = "", int numTimes = 1)
{
var viewModel = new WelcomeViewModel
{
Message = "Hello " + name,
NumTimes = numTimes
};
return View(viewModel);
}
public class WelcomeViewModel
{
public string Message { get; set; }
public int NumTimes { get; set; }
}
and the view in Welcome() is:
<h2>Welcome</h2>
<% for(int i = 0; i < Model.NumTimes; i++) {%>
<h3><%: Model.Message; %></h3>
<%} %>
Firstly, when I run this, I get an error when running .../Welcome?name=Scott&numtimes=4 saying that in the line
<h3><%: Model.Message; %></h3>
it expects ')'
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS1026: ) expected
why is this?
Secondly what is this whole Model thing? What does it do?
It's because the <%: Model.Message; %>
translates (basically) into:
Response.Write(Model.Message;);
As you see, the semicolon should not be there. The compiler expects the ending parentheses before there is a semicolon, hence the error message.
The "Model thing" is the M in MVC. The Model is the data that the View displays. Each view has a single Model, so the Model contains all the data that the View needs.