-1
    public ActionResult Search(string name)
    {

        if (string.IsNullOrWhiteSpace(name))
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        var movie = db.Movies.Where(x => x.Title.Contains(name)).FirstOrDefault();
        if (movie == null)
        {
            return HttpNotFound();
        }
        return View(movie);
    }

I am trying to create a search parameter to pull the data from a table I have created in c#. Running the application takes me to localhost:xxxx, which shows me the entire table. I want localhost:xxxx/movies/search/man to pull my data with that string inside of it.

Here is the RouteConfig.cs

routes.MapRoute(
   name: "Search",
   url: "movies/search/{name}",
   defaults: new { controller = "Movies", action = "Search", name = UrlParameter.Optional }
);

Call stack:

  [InvalidOperationException: The view 'Search' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Movies/Search.aspx
~/Views/Movies/Search.ascx
~/Views/Shared/Search.aspx
~/Views/Shared/Search.ascx
~/Views/Movies/Search.cshtml
~/Views/Movies/Search.vbhtml
~/Views/Shared/Search.cshtml
~/Views/Shared/Search.vbhtml]
   System.Web.Mvc.ViewResult.FindView(ControllerContext context) +382
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +116
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +173
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9748665
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159
mjwills
  • 23,389
  • 6
  • 40
  • 63
Kahawk
  • 7
  • 3

2 Answers2

1

It appears as though you have not got a view for this to render onto.

To add a view (presuming you are using ASP-MVC is to go into your views folder, add a "Movies" folder (if one does not exist) and add a Search.cshtml file under that folder, here's an example: Adding Search view in vs

This answer is a response to the stack trace that you have provided, this doesn't seem related to the title - reading the stack trace it does not seem like Linq is the issue here.

Hope this helps

Mark Davies
  • 1,447
  • 1
  • 15
  • 30
0

public ActionResult Search(string name) {

        if (string.IsNullOrWhiteSpace(name))
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        var movies = db.Movies.Where(x => x.Title.Contains(name)).ToList();
        if (movies == null)
        {
            return HttpNotFound();
        }
        return View("index", movies);
    }

Thank you for your insight. It was a problem with the view. I wasn't pointing to anything. For my purposes, I just pointed back to what I am using as my index.

Kahawk
  • 7
  • 3