asp.net - The name 'model' does not exist in current context - Only on one page at one variable -


so "the name 'model' not exist in current context" error pretty common. however, might have different kind of cause.

in view use multiple model.variablename variables. on 1 line doesn't work:

@html.passwordfor(model => model.pw, new { @value = model.pw}) 

as were

@html.textboxfor(model => model.email) 

just works.

i've tried many, if not all, possible solutions. nothing worked. however, exclude here's part webconfig (main):

<appsettings>     <add key="webpages:version" value="3.0.0.0" />     <add key="webpages:enabled" value="false" />   </appsettings> 

and webconfig in views folder:

<configuration>   <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>    <system.web.webpages.razor>     <host factorytype="system.web.mvc.mvcwebrazorhostfactory, system.web.mvc, version=5.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" />         <add namespace="system.web.optimization" />          <add namespace="winglok" />       </namespaces>     </pages>   </system.web.webpages.razor> 

i'm using asp.net version 4.6, mvc5, visual studio 2012 , razor 3.0 on windows 10 computer, make information complete.

@model won't work.

the reason place @ in front if using c# syntax e.g.

html.inputfor(model => model.name, new { class = "something" }

will fail because class reserved word. need:

html.inputfor(model => model.name, new { @class = "something" }

which correct. now, based on logic, razor compiler looking object instance named model parse this:

@html.passwordfor(model => model.pw, new { @value = @model.pw})

side-note, don't think need @ in front of value.

it cannot find instance named model errors.

in lambda @html.passwordfor(model => model.pw,.... model argument exists in context of labda expression. not available elsewhere on line of code. hence reason why getting error.

to fix problem, try

@html.passwordfor(model => model.pw, new { @value = model.pw})

the variable model should available everywhere in razor file. model actual viewmodel has been passed view itself.

edit:

here code tried works fine me

viewmodel

public class homeindexviewmodel {     public string name { get; set; } } 

controller

public class homecontroller : controller {     public actionresult index()     {         var viewmodel = new homeindexviewmodel();          viewmodel.name = "hello world!";          return view(viewmodel);     } } 

razor view

@model webapplication1.controllers.homeindexviewmodel  @html.textboxfor(model => model.name, new { data_name = model.name }) 

when view web page, see input box "hello world!" in it. also, here's html field:

<input data-name="hello world!" id="name" name="name" type="text" value="hello world!">

as can see, custom data attribute data-name has been populated correctly.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -