locked
MVVM Code cant reach View! RRS feed

  • Question

  • User1421620300 posted

    Any help help would be appreciated in finding a solution to the following error. Markus

    Server Error in '/' Application.


    The view '[{"number":"CREA101","name":"Care of Magical Creatures","instructor":"Rubeus Hagrid"},{"number":"DARK502","name":"Defence Against the Dark Arts","instructor":"Severus Snape"},{"number":"TRAN201","name":"Transfiguration","instructor":"Minerva McGonagall"}]' or its master was not found or no view engine supports the searched locations. The following locations were searched:
    ~/Views/Courses/[{"number":"CREA101","name":"Care of Magical Creatures","instructor":"Rubeus Hagrid"},{"number":"DARK502","name":"Defence Against the Dark Arts","instructor":"Severus Snape"},{"number":"TRAN201","name":"Transfiguration","instructor":"Minerva McGonagall"}].aspx

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.InvalidOperationException: The view '[{"number":"CREA101","name":"Care of Magical Creatures","instructor":"Rubeus Hagrid"},{"number":"DARK502","name":"Defence Against the Dark Arts","instructor":"Severus Snape"},{"number":"TRAN201","name":"Transfiguration","instructor":"Minerva McGonagall"}]' or its master was not found or no view engine supports the searched locations. The following locations were searched:
    ~/Views/Courses/[{"number":"CREA101","name":"Care of Magical Creatures","instructor":"Rubeus Hagrid"},{"number":"DARK502","name":"Defence Against the Dark Arts","instructor":"Severus Snape"},{"number":"TRAN201","name":"Transfiguration","instructor":"Minerva McGonagall"}].aspx
    ~/Views/Courses/[{"number":"CREA101","name":"Care of Magical Creatures","instructor":"Rubeus Hagrid"},{"number":"DARK502","name":"Defence Against the Dark Arts","instructor":"Severus Snape"},{"number":"TRAN201","name":"Transfiguration","instructor":"Minerva McGonagall"}].ascx
    ~/Views/Shared/[{"number":"CREA101","name":"Care of Magical Creatures","instructor":"Rubeus Hagrid"},{"number":"DARK502","name":"Defence Against the Dark Arts","instructor":"Severus Snape"},{"number":"TRAN201","name":"Transfiguration","instructor":"Minerva McGonagall"}].aspx

    Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


    Stack Trace: 

    [InvalidOperationException: The view '[{"number":"CREA101","name":"Care of Magical Creatures","instructor":"Rubeus Hagrid"},{"number":"DARK502","name":"Defence Against the Dark Arts","instructor":"Severus Snape"},{"number":"TRAN201","name":"Transfiguration","instructor":"Minerva McGonagall"}]' or its master was not found or no view engine supports the searched locations. The following locations were searched:
    ~/Views/Courses/[{"number":"CREA101","name":"Care of Magical Creatures","instructor":"Rubeus Hagrid"},{"number":"DARK502","name":"Defence Against the Dark Arts","instructor":"Severus Snape"},{"number":"TRAN201","name":"Transfiguration","instructor":"Minerva McGonagall"}].aspx
    ~/Views/Courses/[{"number":"CREA101","name":"Care of Magical Creatures","instructor":"Rubeus Hagrid"},{"number":"DARK502","name":"Defence Against the Dark Arts","instructor":"Severus Snape"},{"number":"TRAN201","name":"Transfiguration","instructor":"Minerva McGonagall"}].ascx
    ~/Views/Shared/[{"number":"CREA101","name":"Care of Magical Creatures","instructor":"Rubeus Hagrid"},{"number":"DARK502","name":"Defence Against the Dark Arts","instructor":"Severus Snape"},{"number":"TRAN201","name":"Transfiguration","instructor":"Minerva McGonagall"}].aspx
    ~/Views/Shared/[{"number":"CREA101","name":"Care of Magical Creatures","instructor":"Rubeus Hagrid"},{"number":"DARK502","name":"Defence Against the Dark Arts","instructor":"Severus Snape"},{"number":"TRAN201","name":"Transfiguration","instructor":"Minerva McGonagall"}].ascx
    ~/Views/Courses/[{"number":"CREA101","name":"Care of Magical Creatures","instructor":"Rubeus Hagrid"},{"number":"DARK502","name":"Defence Against the Dark Arts","instructor":"Severus Snape"},{"number":"TRAN201","name":"Transfiguration","instructor":"Minerva McGonagall"}].cshtml
    ~/Views/Courses/[{"number":"CREA101","name":"Care of Magical Creatures","instructor":"Rubeus Hagrid"},{"number":"DARK502","name":"Defence Against the Dark Arts","instructor":"Severus Snape"},{"number":"TRAN201","name":"Transfiguration","instructor":"Minerva McGonagall"}].vbhtml
    ~/Views/Shared/[{"number":"CREA101","name":"Care of Magical Creatures","instructor":"Rubeus Hagrid"},{"number":"DARK502","name":"Defence Against the Dark Arts","instructor":"Severus Snape"},{"number":"TRAN201","name":"Transfiguration","instructor":"Minerva McGonagall"}].cshtml
    ~/Views/Shared/[{"number":"CREA101","name":"Care of Magical Creatures","instructor":"Rubeus Hagrid"},{"number":"DARK502","name":"Defence Against the Dark Arts","instructor":"Severus Snape"},{"number":"TRAN201","name":"Transfiguration","instructor":"Minerva McGonagall"}].vbhtml]
       System.Web.Mvc.ViewResult.FindView(ControllerContext context) +499
       System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +143
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +88
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +775
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +81
       System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__5(IAsyncResult asyncResult) +188
       System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38
       System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +26
       System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +73
       System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +52
       System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +39
       System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38
       System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +40
       System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +73
       System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +602
       System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +195
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +128
    Tuesday, December 4, 2018 6:57 PM

Answers

  • User-893317190 posted

    Hi Markus33,

    I find your code 

    public ActionResult Index()
    {
    return View(_registrationVmBuilder.GetSerializedCourses());
    }

    And you code _registrationVmBuilder.GetSerializedCourses() returns a json string.

    If your pass a string to the method View(),it will recognize it as a view name, this is why mvc will try to find the view with the name of your json string.

    To avoid this, you could try the code below.

     public ActionResult Index()
            {
                var courses = new[]
       {
    new CourseVm {Number = "CREA101", Name = "Care of Magical Creatures", Instructor = "Rubeus Hagrid"},
    new CourseVm {Number = "DARK502", Name = "Defence Against the Dark Arts", Instructor = "Severus Snape"},
    new CourseVm {Number = "TRAN201", Name = "Transfiguration", Instructor = "Minerva McGonagall"},
    };
                var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
                var serializedCourses = JsonConvert.SerializeObject(courses, Formatting.None, settings);
               
                return View((Object)serializedCourses);
            }
           
    
            public class CourseVm
            {
                public string Number { get; set; }
                public string Name { get; set; }
                public string Instructor { get; set; }
            }

    My view.

    @model string
    
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    @Model
    

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 6, 2018 1:19 AM

All replies

  • User-893317190 posted

    Hi Markus33,

    What MVVM structure do you use ?

    Specifically,what code causes the access to the view? What's the request address of your code?

    From your error, your request is looking for a view named [{"number":"CREA101","name":"Care of Magical Creatures","instructor":"Rubeus Hagrid"},{"number":"DARK502","name":"Defence Against the Dark Arts","instructor":"Severus Snape"},{"number":"TRAN201","name":"Transfiguration","instructor":"Minerva McGonagall"}].aspx.

    It is a strange name, do you have a view with this name in the folder Courses?

    You could also open F12 , click network  to see the content of your request.

    If you don't have the view, please check your code and change your code to request the correct address.

    If you still have a problem , please  show your related code which causes the problem.

    Best regards,

    Ackerly Xu 

    Wednesday, December 5, 2018 1:56 AM
  • User1421620300 posted

    XU, here is the controller class that calls the Model class that displays into the view! I corrected a few problems but still the same error as before!Markus

    CoursesController

    namespace Angular4DotNetMvc.Controllers
    {
    public class CoursesController : Controller
    {
    private RegistrationVmBuilder _registrationVmBuilder = new RegistrationVmBuilder();

    public ActionResult Index()
    {
    return View(_registrationVmBuilder.GetSerializedCourses());
    }

    }

    ViewModelBuilder class

    namespace Angular4DotNetMvc.Models.Registration
    {
    public class RegistrationVmBuilder
    {
    public RegistrationVm BuildRegistrationVm()
    {
    var registrationVm = new RegistrationVm
    {
    Courses = GetSerializedCourses(),
    Instructors = GetSerializedInstructors()
    };

    return registrationVm;
    }

    public string GetSerializedCourses()
    {
    var courses = new[]
    {
    new CourseVm {Number = "CREA101", Name = "Care of Magical Creatures", Instructor = "Rubeus Hagrid"},
    new CourseVm {Number = "DARK502", Name = "Defence Against the Dark Arts", Instructor = "Severus Snape"},
    new CourseVm {Number = "TRAN201", Name = "Transfiguration", Instructor = "Minerva McGonagall"},
    };
    var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
    var serializedCourses = JsonConvert.SerializeObject(courses, Formatting.None, settings);
    return serializedCourses;
    }

    }

    CoursesView

    Layout = "~/Views/Shared/_Layout.cshtml";
    }

    @section JavascriptInHead
    {
    <script src="~/Scripts/Courses/courses-controller.js"></script>
    <script type="text/javascript">
    registrationModule.factory('bootstrappedCourses', function() {
    return {
    courses: @Html.Raw(Model)
    };
    });
    </script>
    }

    <div class="container" ng-controller="CoursesController">
    <div class="row">
    <div class="span10">
    <h2>Courses</h2>
    </div>
    </div>
    <div class="row">
    <div class="span10">
    <table class="table table-condensed table-hover">
    <tr>
    <th>Course</th>
    <th>Course Name</th>
    <th>Instructor</th>
    </tr>
    <tr ng-repeat="course in courses">
    <td>{{course.number}}</td>
    <td>{{course.name}}</td>
    <td>{{course.instructor}}</td>
    </tr>
    </table>
    </div>
    </div>
    </div>

    Wednesday, December 5, 2018 3:42 PM
  • User-893317190 posted

    Hi Markus33,

    I find your code 

    public ActionResult Index()
    {
    return View(_registrationVmBuilder.GetSerializedCourses());
    }

    And you code _registrationVmBuilder.GetSerializedCourses() returns a json string.

    If your pass a string to the method View(),it will recognize it as a view name, this is why mvc will try to find the view with the name of your json string.

    To avoid this, you could try the code below.

     public ActionResult Index()
            {
                var courses = new[]
       {
    new CourseVm {Number = "CREA101", Name = "Care of Magical Creatures", Instructor = "Rubeus Hagrid"},
    new CourseVm {Number = "DARK502", Name = "Defence Against the Dark Arts", Instructor = "Severus Snape"},
    new CourseVm {Number = "TRAN201", Name = "Transfiguration", Instructor = "Minerva McGonagall"},
    };
                var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
                var serializedCourses = JsonConvert.SerializeObject(courses, Formatting.None, settings);
               
                return View((Object)serializedCourses);
            }
           
    
            public class CourseVm
            {
                public string Number { get; set; }
                public string Name { get; set; }
                public string Instructor { get; set; }
            }

    My view.

    @model string
    
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    @Model
    

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 6, 2018 1:19 AM