-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.

4
  • Check this out: stackoverflow.com/questions/4586336/… Commented Oct 1, 2018 at 5:21
  • 1
    @Url.Action("Index", "ControllerName", new { id = 1, xxx = true }) (where xxx is your boolean property - it cannot be named bool) Commented Oct 1, 2018 at 5:26
  • /controllername/index/1?xxx=True i am getting output like that. but i want like this-> Index?id=1&bool=true Commented Oct 1, 2018 at 5:34
  • If you mean you want the controller name to be omitted, then you cannot unless you write custom route definitions Commented Oct 1, 2018 at 5:42

2 Answers 2

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 })
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

OP wants to generate a url, not submit a form!
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.
No where in the question is OP generating a form (this has nothing to do with the question)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.