0
votes

In my MVC3 application, I have an action that routes to a custom action depending on an object selected.

    public ActionResult SearchCityState(string city, string state, string searchTerm)
    {
        city = Server.HtmlEncode(city);
        state = Server.HtmlEncode(state);
        searchTerm = Server.HtmlEncode(searchTerm);

        // now build the search object
        ...

        return DoSearch(sourceRequestObject);
    }


    public ActionResult SearchState(string state, string searchTerm)
    {
        state = Server.HtmlEncode(state);
        searchTerm = Server.HtmlEncode(searchTerm);

        // now build the search object
        ...

        return DoSearch(sourceRequestObject);
    }

Those two methods do a bit of work in populating an object and calling the following DoSearch() method in the class and are selected based on some logic:

public ActionResult DoSearch(FeederService.SearchRequestObject sourceRequestObject)
{
    ...

    var model = new MyAppMVC.Models.ResultsModel();
    var page = model.GetData(sourceRequestObject);

    return View(page);
}

Here's my model class:

public class ResultsPage
{
    public DataSet dsResults { get; set; }

    public Int32 actualNumberOfResults { get; set; }
    public int numberOfResultsReturned { get; set; }
}

public class ResultsModel
{
    ...

    public ResultsPage GetData(FeederService.SearchRequestObject sourceRequestObject)
    {
        var page = new ResultsPage();

        ...

        page.dsResults = myWcfFeederClient.GetData(sourceRequestObject);

        if (page.dsResults != null)
        {
            page.actualNumberOfResults = Convert.ToInt32(page.dsResults.Tables[1].Rows[0]["ActualNumberOfResults"].ToString());
            page.numberOfResultsReturned = Convert.ToInt16(page.dsResults.Tables[1].Rows[0]["NumberOfResultsReturned"].ToString());
        }

        return page;
    }
}

I have a view defined in /Results/SearchResults.cshtml that I want to route all requests to, as the output will be the same for all

The issue is that the initially selected action name is the default selected view. ie. if SearchCityState() is called, the following exception is thrown:

The view 'SearchCityState' or its master was not found or no view engine supports the searched locations. The following locations were searched:

~/Views/Results/SearchCityState.aspx ~/Views/Results/SearchCityState.ascx ~/Views/Shared/SearchCityState.aspx ~/Views/Shared/SearchCityState.ascx ~/Views/Results/SearchCityState.cshtml ~/Views/Results/SearchCityState.vbhtml ~/Views/Shared/SearchCityState.cshtml ~/Views/Shared/SearchCityState.vbhtml

... and similar for SearchState(). I'm familiar with this issue, but I can't recall how to route all requests to that one view.

Thanks.

UPDATE

Here are the routes I have defined:

        routes.MapRoute(name: "CityHomePage", url: "{city}-{state}", defaults: new { controller = "Home", action = "GeoHomePage" });
        routes.MapRoute(name: "CityStateResults", url: "{city}-{state}/{searchTerm}", defaults: new { controller = "Results", action = "SearchCityState" });

... and off of a link defined as:

<a href="/City-State/">My CityState Link</a>

I'm ending up with the following error:

The view 'SearchCityState' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Results/SearchCityState.aspx ~/Views/Results/SearchCityState.ascx ~/Views/Shared/SearchCityState.aspx ~/Views/Shared/SearchCityState.ascx ~/Views/Results/SearchCityState.cshtml ~/Views/Results/SearchCityState.vbhtml ~/Views/Shared/SearchCityState.cshtml ~/Views/Shared/SearchCityState.vbhtml

1
so just to confirm, you want to display to the same base CSHTML page for each request? From your DoSearch method?Alastair Pitts
Yes, that is correct. The controller logic will always return data in the same format to the strongly-typed view (using ResultsPage).ElHaix

1 Answers

1
votes

Use another overload of the View() method, which takes the view name as the 1st parameter:

public ActionResult DoSearch(FeederService.SearchRequestObject sourceRequestObject)
{
    ...

    var model = new MyAppMVC.Models.ResultsModel();
    var page = model.GetData(sourceRequestObject);

    return View("SearchResults", page);
}

(The MSDN article isn't helpful, but the answer doesn't feel complete)