Monday 23 September 2019

c# - Could not load type System.Web.Mvc.ViewUserControl



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:




@Html.Action("FormLocationEdit", "Shared", new { area = "Shared", options = Model.LocationID });




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(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" %>

<% 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?


Answer



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.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...