开发者

ASP.NET MVC: 'does not contain a definition for' and 3 different errors depending on my changes (please read first sentece of the post)

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 "IEnumerabl开发者_JS百科e" 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜