Reputation: 78
I'm a beginner at programming and I'm trying to build a mvc application that can search a directory and display all the ones found in a view.I have an error message pop up when I search. If someone would tell me what I'm doing wrong or point me in the right direction it would be greatly appreciated.
error message is this:
> The view 'C:\Users\carrick\Downloads' or its master was not found or
> no view engine supports the searched locations. The following
> locations were searched:
> ~/Views/DirectorySearch/C:\Users\carrick\Downloads.aspx
> ~/Views/DirectorySearch/C:\Users\carrick\Downloads.ascx
> ~/Views/Shared/C:\Users\carrick\Downloads.aspx
> ~/Views/Shared/C:\Users\carrick\Downloads.ascx
> ~/Views/DirectorySearch/C:\Users\carrick\Downloads.cshtml
> ~/Views/DirectorySearch/C:\Users\carrick\Downloads.vbhtml
> ~/Views/Shared/C:\Users\carrick\Downloads.cshtml
> ~/Views/Shared/C:\Users\carrick\Downloads.vbhtml
my controller looks like this
public class DirectorySearchController : Controller
{
//
// GET: /DirectorySearch/
public ActionResult Index()
{
return View();
}
public ActionResult GetDirFiles(string directorySearch)
{
//first check directorySearch is a valid path
//then get files
Directory.GetFiles(directorySearch);
ViewBag.message = directorySearch;
return View(ViewBag.message);
}
}
}
and my view
@{
;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>GetDirFiles</title>
</head>
<body>
<div>
<h2>Search Results</h2>
<ul>
<li>@Viewbag.message;</li>
</ul>
</div>
</body>
</html>
Upvotes: 1
Views: 245
Reputation: 1164
This line:
return View(ViewBag.message);
You are telling it to render the view with the name of the directory files, hence why you are getting that error messaging. ViewBag is already passed into your view so you don't need to pass it yourself.
You most likely just want to have the empty parameter call of
return View();
Which will by default return the view of with the name of the method in your controller.
Besides that you are not passing the files to the view, you are passing the path. You will need to do something like this. Note the case of ViewBag(not Viewbag)
Controller:
ViewBag.message = string.Join(",", Directory.GetFiles(directorySearch));
View:
<li>@ViewBag.message</li>
Or you can write a simple loop in your view
Controller:
ViewBag.message = Directory.GetFiles(directorySearch);
View:
@foreach(string file in ViewBag.message)
{
<li>@file</li>
}
Upvotes: 1
Reputation: 12538
In this line
return View(ViewBag.message);
Change it to
return View();
The first argument is the ViewName. ViewBag is passed to the view ambiently/implicitly, so you dont need to pass it on.
Upvotes: 1