0

I spend all day trying to find the solution. I found some but none of it worked for me. Long short the application I am working on is being upgraded from MVC3 (I believe) to MVC5 and ASP.NET Version 4.6.1. The upgrades were being done by some other developer and now I have to fix all issues and bugs he left. The issue I am facing is that I can't load strongly typed view. I invoke the action method from my main view:

<div id="fromLocationEditPartial">
     @Html.Action("FormLocationEdit", "Shared", new { area = "Shared", options = Model.LocationID });
</div>

Here is the action method code I am calling:

 public ActionResult FormLocationEdit(int? options)
    {
        LocationEditorModel model = new LocationEditorModel();

        //DTLocationAutoMetadata locObj;
        DTLocation locObj;

        if (options.HasValue)
        {
            int locationID = options.Value;
            //locObj = AutoMapper.Mapper.Map<DTLocation, DTLocationAutoMetadata>(entities.DTLocation.Where(m => m.LocationID == locationID).FirstOrDefault());
            locObj = entities.DTLocation.Where(m => m.LocationID == locationID).FirstOrDefault();

            if (locObj == null)
            {
                locObj = new DTLocation();
                locObj.SelectLocation = new SelectList(model.GetListOfLocations(), "Value", "Text");
            }
            else
            {
                locObj.SelectLocation = new SelectList(model.GetListOfLocations(), "Value", "Text", locObj.SiteID);
            }
        }
        else
        {
            locObj = null;
        }

        return View("~/Areas/Shared/Views/FormLocationEdit.ascx", locObj);
    }

Here as you can see it suppose to return the strongly typed view with object. Here is how my shared view looks like:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CMDBv3.DTLocationAutoMetadata>" %>
<script type="text/javascript">
   $(document).ready(function () {
    //BindSubmitActions('formLocationEdit');
   })
</script>
<% using (Html.BeginForm("FormLocationEdit", "Shared", FormMethod.Post,  new { area="Shared", id = "formLocationEdit" })) {%>
***here is the condition if (Model!=null) do something else display model data***

In my VS Error list I am having errors:

Error 4 The name 'Model' does not exist in the current context Error 2 The name 'Html' does not exist in the current context

When I run the proj I am getting inner exception:

Could not load type System.Web.Mvc.ViewUserControl"<"CMDBv3.DTLocationAutoMetadata">"

As I understand it is some issues with versioning but I checked all my web.config and it seems that version is fine there. Can someone give me some advice what else could it be if it is not web.config issue?

Vytautas
  • 27
  • 2
  • 9
  • 1
    You might find that its easier to simply create a new MVC 5 Application and copy+paste over the relevant code. Upgrading MVC apps seems harder than it should be, for even 1 version increase, whereas you are jumping from v3 to v5. – Graham Apr 19 '17 at 12:49

2 Answers2

0

I have similar problem in the past when upgrading MVC 3 (or 4) project using ASCX shared user controls inside area into MVC 5 with Razor views enabled by default (missing ViewUserControl namespace), hence I trying to add these lines below on web.config file inside Views directory of that area and working properly then:

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

NB: This change also apply to other area views' web.config containing ASPX page/ASCX controls which inherit System.Web.Mvc.ViewPage/System.Web.Mvc.ViewUserControl.

Then, in that area view's web.config, ensure these lines added between system.web.webPages.razor element:

<system.web.webPages.razor>
    <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>
</system.web.webPages.razor>

To solve "Model doesn't exist" issue, you can change this line in project's web.config:

<add key="webpages:Version" value="1.0.0.0" />

To this line:

<add key="webpages:Version" value="3.0.0.0" />

For further info about upgrading to MVC 5, check MVC 5 Upgrade Guide.

PS: The missing namespaces problem often occurs when web.config file inside Views directory (including area views) not included or not created yet. Also if you're still not sure try setting Copy Local = True in reference properties related with System.Web.Mvc namespace. After all, seems it is easier to create a new predefined MVC 5 project and resolving relevant existing resources into that.

Related issues:

Could not load type 'System.Web.Mvc.ViewUserControl<SOMETYPE>'

"The name 'HTML' does not exist in the current context" in MVC 3 Views

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

Community
  • 1
  • 1
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
0

The way I solved it just simply created cshtml view and use razor syntax:

@model CMDBv3.DTLocation

@using (Html.BeginForm("FormLocationEdit", "Shared", FormMethod.Post, new { 
area = "Shared", id = "formLocationEdit" }))

and that worked for me.

Vytautas
  • 27
  • 2
  • 9