0
votes

I'm having a problem that VS (2015) complains about some things i my .cshtml views.

"The name 'model' does not exist in the current context"

@model Project.MyModel

'HtmlHelper' doesn not contain a definition for a 'TextBoxFor' and no extension method 'TextBoxFor' accepting a first argument of type 'HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

 @Html.TextBoxFor(x => Model.Property)

I can't figure out why it's like this.

My Views/Web.config

<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.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.Optimization"/>
    <add namespace="System.Web.Routing" />
  </namespaces>
</pages>
</system.web.webPages.razor>

My Web.config

pages>
  <namespaces>
    <add namespace="System.Web.Helpers"/>
    <add namespace="System.Web.Mvc"/>
    <add namespace="System.Web.Mvc.Ajax"/>
    <add namespace="System.Web.Mvc.Html"/>
    <add namespace="System.Web.Optimization"/>
    <add namespace="System.Web.Routing"/>
    <add namespace="System.Web.WebPages"/>
  </namespaces>
</pages>

Can anyone see what I'm missing here?

EDIT: It works fine in VS 2013..

2
Have you tried to close and reopen this file? - Farhad Jabiyev
Check all your <bindingRedirect .. newVersion=".." /> against all you version numbers in the web.config file for a version mismatch - user3559349
whats your .Net target framework? - Chandrasekar Kesavan
4.5. Please see my edit. It works fine in VS 2013 - MrProgram
<appSettings> <add key="webpages:Version" value="3.0.0.0" /> Try changing above value 3.0.0.0 to 2.0.0.0 in your web.config. Build solution and open the view. - John

2 Answers

1
votes

I followed this guide and it now works. It looks like I was missing some version upgrades...

http://www.asp.net/mvc/overview/releases/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2

Look in packages.config and check what version of Microsoft.Aso.Net.Mvc you have. Then in Views/Web.config I changed the version number into that in these places (in my case I added Version=5.2.3.0):

<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</system.web.webPages.razor>

<system.web>
<pages
    validateRequest="false"
    pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
    pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
    userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <controls>
    <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
  </controls>
</pages>
</system.web>

I also upgraded the version number from Version=2.0.0.0 -> Version=3.0.0.0 here (Still Views/Web.config):

<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
  <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
1
votes

Build Your Code in static class in folder named it for example helpers enter image description here

then go and add this folder in the web.config like this

add namespace="TheCompeleteApp.Helper"

If it didn't work go and hard coded it in the view that you using this extension method in like this @using FinalProject_0._1.Helper