Domanda

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>

È stato utile?

Soluzione

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

Altri suggerimenti

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.

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top