Alexander Combe
Alexander Combe

Reputation: 61

Html.RenderAction Throws System.Web.HttpException, due to Child Request Failing

I am trying to create a Registration Page in ASP.NET MVC but there is an issue with one Field in particular. The "Country" field, my intention is to read the countries from a locally stored JSON file and render a partial view (which consists of just a Drop Down Box). However, when I run the code and try and navigate to the Registration page I get the following error message:

An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code

Additional information: Execution of the child request failed. Please examine the InnerException for more information.

Looking closer at the Exception I see the following:

$exception {"Execution of the child request failed. Please examine the InnerException for more information."} System.Web.HttpException

InnerException {"The controller for path '/Account/Register' was not found or does not implement IController."} System.Exception {System.Web.HttpException}

Here is the code:

Partial View

using MyWatch.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyWatch.Controllers
{
    public class CountrySearchController : Controller
    {
        [HttpGet]
        [ChildActionOnly]
        public ActionResult CountrySearch()
        {
            try
            {
                using (StreamReader streamReader = new StreamReader("C:\\Users\\Alex Combe\\Documents\\Visual Studio 2015\\Projects\\MyWatch\\MyWatch\\App_Data\\CountryList.json"))
                {
                    string json = streamReader.ReadToEnd();
                    Countries countries = new Countries();
                    countries.countries = JsonConvert.DeserializeObject<IList<Country>>(json);
                    return PartialView(countries);
                }
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine(e.StackTrace);
                return View(new Countries());
            }
        }
    }
}

Main View

<div class="form-group">
    @Html.LabelFor(m => m.Country, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @*@{Html.RenderAction("CountrySearch", "Search");}*@
        @{Html.RenderAction("CountrySearch", "Search");}
    </div>
</div>

Partial View Controller

namespace MyWatch.Controllers
{
    public class CountrySearchController : Controller
    {
        [HttpGet]
        [ChildActionOnly]
        public ActionResult CountrySearch()
        {
            try
            {
                using (StreamReader streamReader = new StreamReader("C:\\Users\\Alex Combe\\Documents\\Visual Studio 2015\\Projects\\MyWatch\\MyWatch\\App_Data\\CountryList.json"))
                {
                    string json = streamReader.ReadToEnd();
                    Countries countries = new Countries();
                    countries.countries = JsonConvert.DeserializeObject<IList<Country>>(json);
                    return PartialView(countries);
                }
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine(e.StackTrace);
                return View(new Countries());
            }
        }
    }
}

Partial View's Model

namespace MyWatch.Models
{
    public class Country
    {
        public string name { get; set; }
        public string code { get; set; }
    }

    public class Countries
    {
        public string SelectedItem { get; set; }
        public IList<Country> countries;
    }
}

Upvotes: 1

Views: 1141

Answers (2)

Ed Mays
Ed Mays

Reputation: 21

The issue here is with the call to Html.RenderAction in your main view. Looking at the MSDN documentation for RenderAction, it expects us to pass in the ActionMethod name and the Controller name:

public static void RenderAction(
    this HtmlHelper htmlHelper,
    string actionName,
    string controllerName = null,
    object routeValues = null
)

As written, your RenderAction call is passing "Search" for the controllerName parameter, however your controller name is CountrySearch, which causes routing to fail with this exception.

There are a couple of ways to work around this, but the simplest would be to update your RenderAction call to reflect the correct Controller name, like so:

@{Html.RenderAction("CountrySearch", "CountrySearch");}

Upvotes: 2

JamesS
JamesS

Reputation: 2300

Try removing [HttpGet]. According to this post Link . [HttpGet] only allows get requests but if there is a post request, it cannot find the method.

Upvotes: 0

Related Questions