简体   繁体   中英

ASP.NET MVC POST Parameter into RouteData

I'm using jQuery to POST an Id to my route, for example:
http://localhost:1234/Load/Delete
with a POST Parameter Id = 1

I'm only using the default route in Global.asax.cs right now:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = "" }
);

When I perform the POST, in my ActionFilter filterContext.RouteData.Values["Id"] is set to the default value of "" . Ideally, I'd like it to use the POST Parameter value automatically if the URL comes up with "" . I know it is posting properly because I can see the value of "1" in filterContext.HttpContext.Request.Params["Id"] .

Is there a way to get RouteData to use a POST parameter as a fall back value?

You can always add another route that does not use this parameter. I have an example in my ASP.Net MVC book at home. I'll update this when I get home :)

But I would suggest renaming id to ObjectId for clarity, either in the route or in your jquery caller.

Couldn't find the reference, but from the looks of it your best bet is to change the name of the id in the entity. Most ASP.Net MVC articles suggest using ConferenceId, PersonId, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM