好的,所以我得到了这段代码:
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; }
}
Welcome() 中的 View 是:
<h2>Welcome</h2>
<% for(int i = 0; i < Model.NumTimes; i++) {%>
<h3><%: Model.Message; %></h3>
<%} %>
首先,当我运行它时,我在运行 .../Welcome?name=Scott&numtimes=4 时遇到错误,在行中说了这一点
<h3><%: Model.Message; %></h3>
它期望')'
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
这是为什么?
其次,整个模型是什么?它有什么作用?
请您参考如下方法:
这是因为 <%: Model.Message; %>翻译(基本上)为:
Response.Write(Model.Message;);
如您所见,分号不应该存在。编译器期望在分号之前有结束括号,因此会出现错误消息。
“模型事物”是 MVC 中的 M。模型是 View 显示的数据。每个 View 都有一个模型,因此模型包含 View 所需的所有数据。
