使用路线数据作为模型过滤器?

发布于 2024-10-15 04:13:05 字数 733 浏览 1 评论 0原文

也许我对这一切的看法都是错误的。
我有一个大表,其中有 3 个“分类”列。这些分类名称是 MVC 的理想控制器/操作名称。
但是
控制器/操作名称所做的只是过滤到表中的某些行,它不会创建要在视图中显示的唯一模型。有没有更好的方法来看待这个问题?

ID | CAT1 | CAT2 | CAT3 | DETAILS |
1  |  a1  |  b1  |  c1  |  foo    |
...
x  |  a2  |  b2  |  c2  |  bar    |
...
n  |  a3  |  b3  |  c3  |  fun    |
...

/a1/b2/c3/ 路由将产生与 /a3/b3/c1 路由相同的“数据对象”。我对 MVC 的(有限)理解在这里没有意义,因为不同的路线应该使用不同的模型。
如何让所有这些操作使用同一视图?

代码类似于

/drills/hydraulic/   --> WHERE(CAT1="a2" AND CAT2="b3")
/loaders/tracked/    --> WHERE(CAT1="x3" AND CAT2="r3")
/hauling/rails/      --> WHERE(CAT1="c8" AND CAT3="b7")

所以所有 3 个 URL 将使用相同的视图,但是不同的控制器/操作。 看来我没有“正确”使用该模式,因为我有很多控制器,但几乎没有任何视图。

Maybe I'm looking at this all wrong.
I've got a large table with 3 'classification' columns. These classification names are ideal controller/action names for MVC.
BUT
All the controller/action name does is filter down to certain rows in the table, it doesn't create unique models to display in a view. Is there a better way to be looking at this?

ID | CAT1 | CAT2 | CAT3 | DETAILS |
1  |  a1  |  b1  |  c1  |  foo    |
...
x  |  a2  |  b2  |  c2  |  bar    |
...
n  |  a3  |  b3  |  c3  |  fun    |
...

A Route of /a1/b2/c3/ will result in the same 'data object' as a route of /a3/b3/c1. My (limited) understanding of MVC isn't making sense here, as different routes should be using different models.
How do I get all these actions to use the same view?

Code would be something like

/drills/hydraulic/   --> WHERE(CAT1="a2" AND CAT2="b3")
/loaders/tracked/    --> WHERE(CAT1="x3" AND CAT2="r3")
/hauling/rails/      --> WHERE(CAT1="c8" AND CAT3="b7")

So all 3 URL's would be using the same View, but are different controllers/actions.
It seems like I'm not using the pattern 'properly' as I've got lots of controllers, but hardly any views.

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

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

发布评论

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

评论(1

洒一地阳光 2024-10-22 04:13:05

听起来您需要在路由中添加一个控制器 - 就像 ClassificationController 一样。它将有一个方法:

public ActionResult Index(string cat1, string cat2, string cat3)
    {
        //...load from table and return data to the view
    }

然后您的路由将如下所示:

routes.MapRoute("ClassificationIndex", "classification/{cat1}/{cat2}/{cat3}", new { controller = "classification", action = "index" });

...您的路由将如下所示:

/classification/cat1/cat2/cat3

现在您有一个由单个控制器处理的单个视图(Index.aspx) (分类控制器)。

这是你所需要的吗?

It sounds like you need to add a single controller into your routing - like a ClassificationController. It would have a single method:

public ActionResult Index(string cat1, string cat2, string cat3)
    {
        //...load from table and return data to the view
    }

Then your routing would look like this:

routes.MapRoute("ClassificationIndex", "classification/{cat1}/{cat2}/{cat3}", new { controller = "classification", action = "index" });

...and your routes would look like this:

/classification/cat1/cat2/cat3

Now you have a single View (Index.aspx) handled by a single Controller (ClassificationController).

Is this what you needed?

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