name 'html' nor name 'model' exist in current context in usercontrol MVC and C#

我的未来我决定 提交于 2019-12-05 06:00:55

问题


I am using Microsoft MVC and C#. I have a usercontrol (example.ascx) created and at the top I'm inheriting System.Web.MVC.ViewUserControl<PostTransferViewModel>

Now, while my model name is appended to ViewUserControl, I get "The name 'Model' does not exist in the current context" and "The name 'Html' does not exist in the current context. If I removed the <PostTransferViewModel> from the end of ViewUserControl then everything works fine, but I need <PostTransferViewModel>.

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PostTransferViewModel>" %>

<div class="postTransferTank">
    <h2>
        Approved Post Transfers</h2>
    <% if (Model.ApprovedPostTransfers.Count() == 0)
       { %>
    <span class="emptyList">There are currently no approved Post Transfers for this tank.</span><br />
    <% } %>
    <% else
        { %>
    <%=Html.DisplayFor(x => x.ApprovedPostTransfers,"PostTransferList") %>
    <% } %>
    <br />
    <%=Html.ActionLink<PostTransferController>(x => x.NewPostTransfer(), "Enter Post Transfer", new { @class = "create-link" })%>
    <br />
    <% if (Model.DraftPostTransfers.Count() != 0)
       { %>
    <h2>
        Draft Post Transfers</h2>
    <%=Html.DisplayFor(x => x.DraftPostTransfers, "PostTransferList") %>
    <% } %>
</div>

回答1:


This forum post has a potential solution to your problem:

Sometimes Intellisense doesn't show up in aspx/ascx/master files if there is a compilation error in the application. If you try running the site and you get a compilation error from ASP.NET, try fixing it and then see if it works.

If the app was previously using ASP.NET MVC Beta and you later changed it to use the fully released version then it's also quite likely that there's a configuration problem. Try comparing the two web.config files in the project (one in the root and one in the Views folder) and see if there are any differences between them.




回答2:


I had the same problem...

In my web.config file I had just the regular namespaces placed in the node...

    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />

Once I added this to my node the error went away...

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

I hope this helps someone out. I was stuck for a couple of hours trying to figure this out.




回答3:


<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v11.0" />
  </parameters>
</defaultConnectionFactory>

Solution for this problem



来源:https://stackoverflow.com/questions/2326312/name-html-nor-name-model-exist-in-current-context-in-usercontrol-mvc-and-c-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!