Rectangle 27 0

.net ASP.NET MVC View throwing CS1061 error related to type of model object passed as ViewDdata to a view?


I had this problem once, I was passing an integer value as the first parameter for the ActionLink method (when the expected value is a string), so I got this error that was caused for the mistyped parameter data type. I just converted the parameter to string and it worked (ie: myModel.integerProperty.ToString()).

Note
Rectangle 27 0

.net ASP.NET MVC View throwing CS1061 error related to type of model object passed as ViewDdata to a view?


<% foreach (var category in model) { %>
    <li>
        <%= Html.ActionLink(category.CategoryName, "List", new {category= category.CategoryName }) %>
    </li>
<% } %>
<%@ ... Inherits="System.Web.Mvc.ViewPage<List<MVC_Basics_1.Models.Category>>" %>

@Benjamin-I changed ViewData to "Model" & changed Inherits. Where is "Model" defined? Also is the article referring to "ViewData" vs "Model" because something changed between when the article was written and MVC 1 RTM? Finally, oddly I've now lost visibility to Html.ActionLink: "Compiler Error: CS1928: 'System.Web.Mvc.HtmlHelper<System.Collections.Generic.List<MVC_Basics_1.Models.Category>>' does not contain a definition for 'ActionLink' and the best extension method overload 'System.Web.Mvc.Html.LinkExtensions.ActionLink(System.Web.Mvc.HtmlHelper, string, string)' has some invalid arguments".

@Howiecamp: I've corrected the ActionLink parameters. It should work now

An interface such as IEnumerable is a better choice.

I'd love to accept both your answers but apparently not possible. I'm going to accept the other answer because I think both are equally good but he answered first. I needed some criteria!

ViewData is a hash table, using Key-Value pairs, so iterating through ViewData will give you each object in the hash table, not your Model that you're declaring and passing to the View.

You need to be using 'model' in place of ViewData.

Note