Pure.Krome
Pure.Krome

Reputation: 86967

Weird problem when Posting to an ASP.NET MVC action

I've got a simple html form with a few input boxes. When i click save, it finds the correct method but the data is weird. When i have a form field name that is the same name as a field in the route, the value passed in is my form field data, not the route data.

for example.

Imagine u have the following route.

// Both Get/Post
routes.MapRoute(
    "User-Edit",
    "user/{displayName}/edit",
    new { controller = "Account", action = "edit" });

and the following method...

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit([Bind(Exclude = "UserId")]string displayName, 
                               FormCollection formValues)

{ ... }

Now, notice how the route has the variable displayName and the controller action also has a argument called displayName? Well, the argument data is that from the form, NOT the route.

I'm not sure how i can make sure the argument data is the route data?

Is the only fix here for me to rename the route variable, from displayName to routeDisplayName or whatever.. ?

Upvotes: 0

Views: 130

Answers (1)

mikerennick
mikerennick

Reputation: 395

The ModelBinding conventions stipulate that a parameter is populated from:

  • a request.form value if it exists (yours does!)
  • then, RouteData.Values
  • then request.querystring
  • then null

You would have to (a) override this default behavior or (b) rename your route value.

I would go with b.

Mike

Upvotes: 1

Related Questions