好的,所以我得到了以下代码:

    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()中的视图为:

<h2>Welcome</h2>

<% for(int i = 0; i < Model.NumTimes; i++) {%>

    <h3><%: Model.Message; %></h3>
<%} %>


首先,当我运行此命令时,在运行... / Welcome?name = Scott&numtimes = 4时我收到一条错误消息:

<h3><%: Model.Message; %></h3>


它期望')'


  说明:编译服务于此请求所需的资源期间发生错误。请查看以下特定的错误详细信息,并适当地修改您的源代码。
  编译器错误消息:CS1026:)


为什么是这样?



其次,这整个模型是什么?它有什么作用?

最佳答案

这是因为<%: Model.Message; %>可以(基本上)转换为:

Response.Write(Model.Message;);


如您所见,分号不应在那里。编译器在出现分号之前需要结尾括号,因此会出现错误消息。

“模型事物”是MVC中的M。模型是视图显示的数据。每个视图都有一个模型,因此模型包含视图所需的所有数据。

09-10 02:10
查看更多