Grofit
Grofit

Reputation: 18465

asp mvc seemingly overwritting url parameter with model parameter

I have a simple route like so:

some-domain.com/something/<some-guid>/somethingelse

Then my controller looks like:

public class SomeController : Controller
{
    public ActionResult DoSomething(Guid someGuid, SomeModel someModel)
    {...}
}

The routing config looks like:

/something/{someGuid}/somethingelse

Problem is if I send over a url with the guid in the right place it always seems to be an empty guid even though on the querystring is it perfectly fine. The only thing I can think of is that my SomeModel has a property called SomeGuid in this instance, which is empty.

So is mvc somehow taking the someGuid from the model and not the url or is there anything else to check? as I can debug and it hits the right controller from the route, and the url seems fine its just the url parameter seems to default to whatever is in the model.

Upvotes: 1

Views: 194

Answers (2)

Daniel J.G.
Daniel J.G.

Reputation: 35022

That looks like a POST action, in that case you need to be aware that posted form data takes precedence over the route parameters. So if within the posted data for your SomeModel there is an empty Guid with the same name someGuid, it will be used in the binding.

Check this answer for the order in which MVC will check the value providers during the binding process.

You can also check RouteData.Values["someGuid"] and Request.Form["someGuid"] within the controller method. If you have an empty Guid with that name within the form data, then it will be used for your controller method parameter.

Upvotes: 2

gery128
gery128

Reputation: 96

Have you checked RouteData ? Try renaming Model property to something else. MVC routing engine by default searches everything to find required parameters of Route.

Upvotes: 0

Related Questions