-1

I want to generate an URL from my MVC application Razor view which will contain multiple parameter inputs like this: example.com/Index?id=1&bool=true

I already tried this: @Url.Action("Index?id=1&bool=true", "MyController") which however does not work at all.

I would like to ask if there are any ideas how I can get an output like in the example above? Thank you in advance.

Battle
  • 786
  • 10
  • 17
John Doe
  • 317
  • 1
  • 3
  • 8
  • Check this out: https://stackoverflow.com/questions/4586336/sending-multiple-parameters-to-actions-in-asp-net-mvc – Praneet Nadkar Oct 01 '18 at 05:21
  • 1
    `@Url.Action("Index", "ControllerName", new { id = 1, xxx = true })` (where `xxx` is your `boolean` property - it cannot be named `bool`) –  Oct 01 '18 at 05:26
  • /controllername/index/1?xxx=True i am getting output like that. but i want like this-> Index?id=1&bool=true – John Doe Oct 01 '18 at 05:34
  • If you mean you want the controller name to be omitted, then you cannot unless you write custom route definitions –  Oct 01 '18 at 05:42

2 Answers2

0

Try saving the input in hidden fields so when you will navigate to other page, the data will automatically be sent via query string. e.g

@Html.Hidden("Id", Model.Id);

the Id will be sent in the query string

Ali Hasan
  • 174
  • 1
  • 17
  • OP wants to generate a url, **not** submit a form! –  Oct 01 '18 at 05:27
  • Thanks for identifying, what I need to tell him was, that he should save the items in the hidden fields which he wants in the query string. – Ali Hasan Oct 01 '18 at 06:18
  • No where in the question is OP generating a form (this has nothing to do with the question) –  Oct 01 '18 at 06:19
0

You will need to change your default Route RouteConfig.cs:

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

with

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

And then use your URL.Action:

@Url.Action("Index", "ControllerName", new { id = 1, @bool = true })
Yanga
  • 2,885
  • 1
  • 29
  • 32