1

I am having trouble with using attribute routing in ASP.NET MVC 5. Here is the action im using in my controller:

[HttpGet,Route("Home/ChangeID/{MovieInput}")]
public ActionResult ChangeID(int MovieInput)
{
    return View();
}

Here is the form I am using to send the parameter to this action:

<form method="get" action="@Url.Action("ChangeID", "Home")">
    <label for="movieInput">Change an ID: </label>
    <input type="text" id="MovieInput" name="MovieInput" placeholder="Enter Your ID" />
    <input type="submit" />

</form>

The route works perfectly with a URL such as

/Home/ChangeID/65

however it does not support the form submission parameter typing where its

/Home/ChangeID?MovieInput=65`. 

How can I change the form submission to meet the latter or is there a way to change the route to satisfy a parameter typed this way? `

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • 1
    If you change route to `Route("Home/ChangeID")` it will work for the form action. or make the route parameter optional `Route("Home/ChangeID/{MovieInput?}"` (note the `?`) it should have the same effect. The advantage of the second option over the first is that it will allow both `/Home/ChangeID/65` and `/Home/ChangeID?MovieInput=65` – Nkosi Jun 11 '19 at 00:35
  • Possible duplicate of [Query string not working while using attribute routing](https://stackoverflow.com/questions/22642874/query-string-not-working-while-using-attribute-routing) – Jonathon Chase Jun 11 '19 at 00:45

1 Answers1

1

If you change route template to Route("Home/ChangeID")

//GET /Home/ChangeID?MovieInput=65`
[HttpGet,Route("Home/ChangeID")]
public ActionResult ChangeID(int MovieInput) {
    return View();
}

it will work for the form action or make the route parameter optional Route("Home/ChangeID/{MovieInput?}") (note the ?)

//GET /Home/ChangeID/65`
//GET /Home/ChangeID?MovieInput=65`
[HttpGet,Route("Home/ChangeID/{MovieInput?}")]
public ActionResult ChangeID(int MovieInput) {
    return View();
}

it should have the same effect.

The advantage of the second option over the first is that it will allow both /Home/ChangeID/65 and /Home/ChangeID?MovieInput=65 to match the controller action.

Nkosi
  • 235,767
  • 35
  • 427
  • 472