Model.(变量)有什么作用?

发布于 2024-10-08 08:39:51 字数 952 浏览 1 评论 0原文

好的,我已经得到了这段代码:

    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:)预期

这是为什么?


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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月下客 2024-10-15 08:39:51

这是因为 <%: Model.Message; %> 翻译(基本上)为:

Response.Write(Model.Message;);

如您所见,分号不应该在那里。编译器期望在分号之前有结束括号,因此会出现错误消息。

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

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.

嗼ふ静 2024-10-15 08:39:51

我认为您不需要在 Model.Message 之后添加分号。

模型是对您在控制器中提供给视图的内容的引用。这与您在控制器中键入 return View(viewModel); 的实例相同。

I think you don't need to put the semi-colon after Model.Message.

Model is the reference to what you supplied to your view, in your controller. That's the same instance as what you type return View(viewModel); in your controller.

眼泪也成诗 2024-10-15 08:39:51

关于你的第二个问题,MVC 是一种将逻辑(在你的控制器中)与表示(在你的视图中)分离的方法。

您使用控制器生成一个模型,其中包含视图所需的所有信息。

例如,对于表单,模型将为每个输入都有一个字段。对于表,它将有一个 IEnumerable 等...

视图本身应该尽可能少地进行处理 - 简单的 if 语句和循环。所有实际逻辑都应限制于控制器。

一种思考方式是,开发人员编写控制器,设计人员编写视图,他们就模型中的内容进行协作 - 设计人员说“我需要知道 X、Y、Z” - 因此开发人员将它们添加到模型中模型并根据需要填充字段

正如另一个答案中提到的,Model.Message后面的分号是多余的。

With regards to your second question, MVC is a way of separating the logic (in your controller) from the presentation (in your view).

You use the controller to generate a model which contains all the information required by the view.

Eg for a form, the model would have a field for each input. For a table, it would have an IEnumerable<SomeRowClass> etc...

The view itself should do as little processing as possible - simple if statements and loops. all actual logic should be constrained to the controller.

One way to think of it is that a developer writes the controller, a designer writes the view and they collaborate for what goes in the model - designer says "I need to know X,Y,Z" - so the developer adds them to the model and populates the fields as appropriate

As mentioned in the other answer, the semicolon after Model.Message is superfluous.

拍不死你 2024-10-15 08:39:51

除了放错位置的分号之外,请确保您的视图定义为继承自 ViewPage。这指定了 Model 在视图中的类型,允许编译器解析其成员(MessageNumTimes)。

In addition to the misplaced semicolon, make sure your view is defined to inherit from ViewPage<WelcomeViewModel>. This specifies the type that Model has within the view, allowing the compiler to resolve its members (Message, NumTimes).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文