ASP.Net MVC have view link to a different controller

I have an ASP.Net MVC web application. I have several views and controllers. How do you change what controller a view is tied too? For example, I created a view that referenced code in the HomeController. I created a new controller to house that code only, but I can't get the view to use the new controller instead of the HomeController. I hope that makes sense. Thanks for any help.
dodgerfanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Snarf0001Commented:
Doesn't quite make sense: The controller comes first, and points to a specific view.  So the view can be referenced by any number of controllers in the system, and isn't "tied" to any one of them.

Are you asking how to POST data from one view to a different controller?
Or how to reference a different view from a different calling controller?

Any more details or specific examples of what you're trying to accomplish would help.
dodgerfanAuthor Commented:
Thanks, I'm was worried I was not stating the issue well. I have a HomeController with code to call an API. I created a Search.cshtml view page to call that code. That works. However, I want to take that code in the HomeController and create a different controller (SearchController) to house that code. But when I move the code to the new SearchController and remove it from Home, the Search.cshtml no longer works. If I move the code back to Homer, the Search.cshtml page works.  I hope this helps. Here is my HomeController with the code I want to move to Search:
using System;
using System.Collections.Generic;
using PlayerSearchAPIClient.Models;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace PlayersAPIClient.Controllers
{
    public class HomeController : Controller
    {
        string Baseurl = "http://localhost:3000/";
        public async Task<ActionResult> Index()
        {
            List<Player> PlayerInfo = new List<Player>();

            using (var client = new HttpClient())
            {
                //Passing service base url  
                client.BaseAddress = new Uri(Baseurl);

                client.DefaultRequestHeaders.Clear();
                //Define request data format  
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                //Sending request to find web api REST service resource GetAllPlayers using HttpClient  
                HttpResponseMessage Res = await client.GetAsync("api/players");

                //Checking the response is successful or not which is sent using HttpClient  
                if (Res.IsSuccessStatusCode)
                {
                    //Storing the response details recieved from web api   
                    var playerResponse = Res.Content.ReadAsStringAsync().Result;

                    //Deserializing the response recieved from web api and storing into the Employee list  
                    PlayerInfo = JsonConvert.DeserializeObject<List<Player>>(playerResponse);

                }
                //returning the employee list to view  
                return View(PlayerInfo);
            }
        }

        [HttpPost]
        public async Task<ActionResult> Search(string searchterm)
        {
            List<Player> PlayerInfo = new List<Player>();
            using (var client = new HttpClient())
            {
                //Passing service base url  
                client.BaseAddress = new Uri(Baseurl);

                client.DefaultRequestHeaders.Clear();
                //Define request data format  
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                //Sending request to find web api REST service resource GetAllPlayers using HttpClient  
                HttpResponseMessage Res = await client.GetAsync("api/players/find/"+searchterm);

                //Checking the response is successful or not which is sent using HttpClient  
                if (Res.IsSuccessStatusCode)
                {
                    //Storing the response details recieved from web api   
                    var playerResponse = Res.Content.ReadAsStringAsync().Result;

                    //Deserializing the response recieved from web api and storing into the Employee list  
                    PlayerInfo = JsonConvert.DeserializeObject<List<Player>>(playerResponse);

                }
                //returning the employee list to view  
                return View(PlayerInfo);
            }
        }

        public async Task<ActionResult> Search()
        {
             return View();            
        }
    }
}

Open in new window

Snarf0001Commented:
But when I move the code to the new SearchController and remove it from Home, the Search.cshtml no longer works.

Why not?  What happens or what error do you get?
Did you move the view to a "Search" folder as well?
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

dodgerfanAuthor Commented:
There was no error, the page just refreshed. But, I had not created a Search folder under views. I created one and moved the Search.cshtml page there and now it works. I had the search view in the Views/Home folder. Now I have Views/Home and Views/Search. Do I need to do it that way? Can I just have a Home folder with all of my views? Or an can I just have a Views folder with all of the views? Thanks for the help.
Snarf0001Commented:
That's the default way the routing engine works, to have each view underneath a folder with the same controller name.
You can override that, by changing the default routing, or by specifying the view in the controller, ie return View("/path/view", model),
but honestly I wouldn't bother.
It works well the default way, and is the accepted standard.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dodgerfanAuthor Commented:
Great, thanks for the help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.