ASP.NET MVC 4 Razor view not recognizing Dropdownlistfor HTML Helper
Asked Answered
J

3

6

I am trying to add a dropdownlist to a strongly typed razor view. ASP.Net MVC 4.0, Razor View engine version 2.0.0.0

@using System;
@model SampleApp.Models.ServiceRequestModel

@{
  ViewBag.Title = "ServiceRequest";
}

@Html.DropDownListFor(m=>m.CategoryID, Model.Categories)

and the Model is as below:

public class ServiceRequestModel
{
    public int ID { get; set; }
    public int CategoryID { get; set; }

    public SelectList Category { get; set; }
}

it is always showing an error in intellisense in CSHTML file as:

System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'DropDownListFor' and no extension method 'DropDownListFor' accepting a first argument of type 'System.Web.WebPages.Html.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

and also it is giving errors for :

Error 3 The name 'model' does not exist in the current context

I have checked the web.config in View folder:

<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
  </namespaces>
</pages>

Jaques answered 7/11, 2012 at 11:7 Comment(3)
Did you find any solution for this problem yet?Untune
Yes this is solved. The line for Host factory in the above config is pointing to a wrong version , it should be 3.0.0.0 as below: <system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />Jaques
Would you mind providing this as an answer? It will help the users with similar problems.Untune
J
1

The below line of config code had to be changed to 4.0.0.0

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, **Version=3.0.0.0**, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

changed to

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, **Version=4.0.0.0**, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
Jaques answered 26/11, 2012 at 5:49 Comment(0)
M
1

I spent over a day on this error and it ended up being a data type clash with the VM data source for the dropdownlist (ie it wasn't a list of type IEnumerable). For some reason VS2012 thought the error was with the namespace even though it appeared in Intellipath.

Magneton answered 27/11, 2013 at 3:29 Comment(0)
N
0

I had this exact issue (only with html.displayFor).

I'm not sure how it started but i solved it by replacing the following:

<appSettings>
    <add key="webpages:Enabled" value="false" />
</appSettings>

with

<appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
</appSettings>

in the View folder's Web.config file.

Found my solution here

Necrophilism answered 30/11, 2015 at 1:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.