MyDaftQuestions
MyDaftQuestions

Reputation: 4701

MVC does not render view if querystring is used

I have an ASP.NET MVC website. The issue I am having is if I add a querystring, it can't render the view

My controller is

public class UpgradeController : Controller
{
    public ActionResult Index(string id)
    {
        return View(id);
    }
}

Simple as that

If I navigate to

localhost:123456/upgrade/index

Then the view is rendered in the browser as expected.

As you can see in the controller, the view Index takes a string parameter called id.

This should mean the following URL will return the same view

localhost:123456/upgrade/index/abc

Sadly, it does not render the expected view. Instead I see

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

The same is true with the following URL

http://localhost:53081/upgrade/index?id=abc

I have only 1 route

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

I am totally baffled as to why it's trying to render a view and treating it like a parameter

Upvotes: 0

Views: 92

Answers (2)

MyDaftQuestions
MyDaftQuestions

Reputation: 4701

This is what I actually did

public ActionResult Index(string id)
{
    return View(model:id);
}

Upvotes: 0

Hari Lubovac
Hari Lubovac

Reputation: 622

Try removing the id arg from this line

    return View(id)

and pass it to the view (if you even need it there) using a different method. see https://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/various-ways-to-pass-data-from-controller-to-view-in-mvc/

passing a string value to View method is obviously done with different intentions: without it, the name of the view file is implied from the name of the action method, while with that you have flexibility to load any view that you want.

Upvotes: 1

Related Questions