Eric J.
Eric J.

Reputation: 150158

Query String Parameter used as View Name

I have some code like this

public ActionResult BookNav(string activeId)
{
    return PartialView(activeId);
}

and attempt to make an Ajax call to that action. I form the callback URL using

@Url.Action("BookNav", "Home", new { activeId = "navHome" })

which yields the URL

http://localhost:7268/Home/BookNav?activeId=navBios

I get an HTTP 500 during the callback. To simplify matters, I opened a new tab in my browser and paste in that URL. The result is:

The partial view 'navBios' was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/navBios.aspx
~/Views/Home/navBios.ascx
...

If, however, I paste the URL without a query string parameter

http://localhost:7268/Home/BookNav

I get the expected output.

Why is the value in my query string being used to pick the view name and how can I fix it?

Upvotes: 0

Views: 75

Answers (1)

user3559349
user3559349

Reputation:

Your passing a string to the return PartialView() which uses the overload that expects a view name (a string) as its parameter. You need to pass the parameter as an object

public ActionResult BookNav(string activeId)
{
    return PartialView((object)activeId);
}

Upvotes: 2

Related Questions