Home Html.RenderAction Throws System.Web.HttpException, due to Child Request Failing
Reply: 2

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

Alexander Combe
1#
Alexander Combe Published in 2018-01-12 11:31:41Z

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;
    }
}
JamesS
2#
JamesS Reply to 2018-01-12 14:47:29Z

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.

mrtig
3#
mrtig Reply to 2018-01-13 16:48:20Z

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");}

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.32958 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO