ASP.NET MVC:“不包含定义”以及 3 个不同的错误,具体取决于我的更改(请阅读帖子的第一句)

发布于 2024-11-09 00:53:55 字数 791 浏览 4 评论 0 原文

我的问题与此严格相关 ASP.NET MVC View throwing CS1061 error related to type of model object located as ViewDdata to a view ,但我的声誉太小,无法在那里添加评论,当我将问题添加为“答案”时,它被删除了......所以请阅读上面提到的问题(线程)及其答案。我按照建议的步骤操作,但由于以下错误,它对我不起作用:

(我将发布屏幕截图来向您展示代码和错误。与 Howiecamp 的相比,变量等的名称已更改代码,但其余部分是相同的,因为它与该教程中的步骤相同。)

好的,这就是当我在“foreach”循环中有“ViewData”时发生的情况: 屏幕截图 1 i

如果我按照您的指示将其更改为“模型”,突然 Visual Studio 无法理解“foreach”: 截图 2

如果我像你建议的那样将“IEnumerable”放在第一行,它确实可以理解,但之后它就停止识别“Html.ActionLink”:屏幕截图3:edge.imgur.com/wcgtI.jpg(抱歉,作为新用户我只能发布2个超链接:|)

在这种情况下我应该做什么?

my question is strictly connected with this one ASP.NET MVC View throwing CS1061 error related to type of model object passed as ViewDdata to a view , but my reputation is to small to add a comment there and when I added my question as an 'answer' it was deleted... So please read the question (thread) mentioned above and the answer for it. I followed the steps suggested but it didn't work for me because of the following errors:

(I will post screenshots to show you both the code and the errors. The names of the variables etc. are changed in comparison to Howiecamp's code but the rest is the same because it's the same step in that tutorial.)

Ok so this is what happens when I have "ViewData" in "foreach" loop: screenshot 1

iIf I change it to "Model" like you instructed, suddenly Visual Studio doesn't understand the "foreach": screenshot 2

It does understand it if I put "IEnumerable" in the first line like you suggested, however after that it stops recognizing "Html.ActionLink": screenshot 3: edge.imgur.com/wcgtI.jpg (sorry, as a new user I can post only 2 hyperlinks :| )

What should I do in this case?

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

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

发布评论

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

评论(1

渡你暖光 2024-11-16 00:53:55

实际上我在 那篇文章是我一开始提到的(我一定是瞎了眼才没有看到它......)

抱歉大家打扰了。但由于我花了你一些时间,也许我也会给出这个问题的正确答案(这给了 Benjamin Anderson如果可以的话,我会给他声誉点,这在这里非常珍贵:))所以如果其他人关注该教程 并停留在同一阶段,他会在这里找到答案。

显然,问题的原因是 Scott 当时使用的是旧版本的 MVC。因此,如果您希望它起作用,您必须:

1)而不是“ViewData”,而是放置“Model”,

而不是

foreach (var kategoria in ViewData)

放置

foreach (var kategoria in Model)

2)在第一行中,您必须添加 > >

而不是

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SklepAlfa.Models.Kategorie_produktow>" %>

put

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<SklepAlfa.Models.Kategorie_produktow>>" %>

3) 更改您的 ActionLink 定义!

而不是

<%= Html.ActionLink(category.CategoryName, new { action="List", category=category.CategoryName }) %>

put

<%= Html.ActionLink(category.CategoryName, "List", new {category= category.CategoryName }) %>

情况就是如此。

Actually I found my answer in the comments of that article I was referring to at the beginning (I must had been blind not to see it...)

Sorry everyone for bothering you. But as I took some of your time than maybe I will give also the correct answer for this problem (which gave Benjamin Anderson and if I could I would give him reputation point which are so precious here :) ) so if anyone else followed that tutorial and stuck on the same stage he would find the answer here.

Apparently the cause of the problem was that Scott was using an older version of MVC at that time. So if you want this to work you have to:

1) Instead of "ViewData" put "Model"

Instead of

foreach (var kategoria in ViewData)

put

foreach (var kategoria in Model)

2) In the first line you have to add >

Instead of

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SklepAlfa.Models.Kategorie_produktow>" %>

put

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<SklepAlfa.Models.Kategorie_produktow>>" %>

3) Alter your ActionLink definition!

Instead of

<%= Html.ActionLink(category.CategoryName, new { action="List", category=category.CategoryName }) %>

put

<%= Html.ActionLink(category.CategoryName, "List", new {category= category.CategoryName }) %>

That was the case.

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