piiiiiiiiiiiii
piiiiiiiiiiiii

Reputation: 69

Issue with Attribute Routing working with traditional form parameter

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? `

Upvotes: 1

Views: 113

Answers (1)

Nkosi
Nkosi

Reputation: 247123

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.

Upvotes: 1

Related Questions