Search code examples
asp.net-mvc-4razor-2

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>


Solution

  • 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" />