Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>

like image 967
Nisha_Roy Avatar asked Nov 07 '12 11:11

Nisha_Roy


2 Answers

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" />
like image 131
Nisha_Roy Avatar answered Oct 20 '22 03:10

Nisha_Roy


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.

like image 32
SteveCav Avatar answered Oct 20 '22 01:10

SteveCav