Domanda

I use AttributeRouting to set specific route for my ActionResult. I got an 404 page not found when I have this configuration:

[GET("Tender/SubmitBid/{id}/{bidId}")]
public ActionResult SubmitBid(string id, string bidId)
{ 
 ...
 return View(model);
}

@using ("SubmitBid", "Tender", new { id = Model.TenderId, bidId = Model.BidId }, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
 ...    
 <button type="submit">Save</button>
}

// 404 not found
[HttpPost]
public ActionResult SubmitBid(BidViewModel model)
{
 ...
}

I installed a url sniffer to see the url trigger the 404 page not found and I got this: http.../Tender/SubmitBid/1/0

It supposed to be working... but I have to remove the latest parameters to reach the ActionResult and I don't know why.

Thank you for your help,

Karine

Edit If I remove the attribute [GET("Tender/SubmitBid/{id}/{bidId}")] the page is accessible for the POST request. But the url is like http...//Tender/SubmitBid/1?bidId=0

È stato utile?

Soluzione

You should not need the query string parameters since they exist in the BidViewModel you post. The point of a POST request is that you don't have query string parameters.

I think you have to use this overload of the Html.BeginForm method:

@using (Html.BeginForm("SubmitBid", "Tender", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.HiddenFor(model => model.Id)
    @Html.HiddenFor(model => model.BidId)

    // Other properties..

    <button type="submit">Save</button>
}

Now it will post to http:localhost/Tender/SubmitBid with the properties of BidViewModel as post values, which contain Id and BidId. The signature of the POST action can stay the same:

[HttpPost]
public ActionResult SubmitBid(BidViewModel model)
{
    string id = model.Id;
    string bidId =  model.bidId;

    // ...
}

It's also possible that AttributeRouting causes this issue. Can you try this with native ASP.NET MVC routing? You could use this specific route for submitting bids:

routes.MapRoute(
    name: "SubmitBid",
    url: "Tender/SubmitBid/{id}/{bidId}/",
    defaults: new 
                { 
                    controller = "Tender", 
                    action = "SubmitBid", 
                    id = UrlParameter.Optional, 
                    bidId = UrlParameter.Optional 
                });
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top