我的 ASP.NET MVC 应用程序中有一个 ViewModel 与视图强绑定,现在控制器怎么样

发布于 2024-09-07 20:39:08 字数 231 浏览 3 评论 0原文

就像标题所说,我在我的 asp.net mvc 应用程序中创建了一个视图模型,因此它将强类型化到我的视图。我的视图模型是两个模型类的组合。现在,当用户点击该视图上的保存按钮时,它将转到控制器。它如何知道要转到哪个控制器?我构建了我的控制器 1 - 1,以便用我的模型和视图说话,这样控制器 A 知道模型 A,控制器 B 知道模型 B。但是如果我有一个视图模型 AB,它如何知道 subit 转到 A或 B. 我需要一个名为 AB 控制器的控制器吗?

So like the title says, I created a view model in my asp.net mvc application so it would be strongly typed to my view. My view model is a combination of two of my model classes. Now when the user hits the save button on that view, it goes to a controller. How does it know what controller to go to? I built my controller 1 - 1 so to speak with my models and views so controller A knows about Model A and controller B knows about Model B. But if I have a view model that is AB how does it know on subit to go to A or B. Do I need a controller called AB Controller?

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

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

发布评论

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

评论(3

高速公鹿 2024-09-14 20:39:09

调用的控制器和操作不依赖于您在初始视图呈现期间用于绑定页面的视图模型对象。调用的控制器(和操作)由发送的请求的 URL 确定。 (您定义的路由之一将根据请求的 URL 字符串进行匹配,否则将返回 404 未找到错误。)

HTML 表单(通常是 POST)中的提交按钮将具有一个 action 属性,用于确定其url 目标,锚标记将具有 href 等。

在您有自定义视图模型对象的情况下,您可以定义一个期望的操作,并尝试通过将其指定为操作的参数来解析该特定类型的对象:

public ActionResult SaveSystemSetting(SystemAdminVM item) {

The controller and action invoked do not depend on the viewmodel object you used to bind the page during initial view rendering. The controller (and action) invoked is determined by the URL of the request sent. (One of the routes you have defined will be matched based on the URL string of the request or a 404 not found error will be returned.)

A submit button in an HTML form (usually a POST) will have an action attribute that determines its url target, an anchor tag will have an href, etc.

In your situation where you have a custom viewmodel object you can define an action to expect and to attempt to parse that specific type of object by specifying it as the parameter to your action:

public ActionResult SaveSystemSetting(SystemAdminVM item) {
给妤﹃绝世温柔 2024-09-14 20:39:09

操作方法不接收模型。它接收参数。

无论如何,我想我知道你来自哪里:你的操作的参数之一具有视图中使用的 ViewModel 的类型。这是一种常见的布局,而且很有意义。在许多项目中,它被广泛使用,以至于一段时间后您开始认为某个操作实际上接收了一个模型。

请记住,您的操作的参数可以是由 ModelBinder 填充的任何参数。您可以将两个模型作为参数传递,可以传递聚合模型 a 和 b 的 ViewModel,也可以传递具有 a + b 属性的 ViewModel。
聚合是最常见的方法。

An Action Method doesn't receive a model. It receives parameters.

Anyway I think I know where you come from: One of the parameters of your action has the type of the ViewModel used in the View. That is a common layout and makes a lot of sense. In lots of projects it is so widely used that after some time you start to think that an action actually receives a model.

Just remenber that the parameters of your action can by anything that can by filled by the ModelBinder. You could pass both Models as parameters, you could pass a ViewModel that agregates Model a and b or you could pass a ViewModel that has properties of a + b.
Agregation ist the most common approach.

稚然 2024-09-14 20:39:09

通常,我们会在控制器中重载操作方法,因此如果您有一个名为 edit 的操作,它使用 viewmodel 对象呈现视图,那么您将拥有一个重载的编辑操作方法,并为该方法指定了 HttpPost。

此方法的数据将作为表单值集合值传递,或者如果您愿意,您可以将其绑定到视图模型对象并进一步处理它。

Generally we overload our action methods in the controller so if you have an action called edit that renders the view with your viewmodel object, you will have an overloaded edit action method with the HttpPost specified for that method.

The data to this method will be passed as form value collection values or you can bind it to your viewmodel object if you like and process it further.

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