1
votes

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

1
Which request is not working. The GET or POST? - Henk Mollema
Can you show the post request and the BidViewModel class? - Henk Mollema
Hi Henk, I can but I don't think it will be usefull. BidViewModel has public int BidId and public int TenderId with some other information and all of them use an Html.HiddenFor to store the value between the GET and the POST. And the POST request is in the main content: // 404 not found [HttpPost] public ActionResult... Like I said in the Edit section if I only use one parameter instead of two the ActionResult will be triggered. I can reproduce the same thing each time I have two parameters or more to pass. - Karine
Why do you want query string parameters in the POST url? The point of a POST is that you don't have them. It's working with one parameter because it uses the default route. ({controller}/{action}/{id}) - Henk Mollema
I'm sorry my sentence is maybe not clear. I do not want to receive those parameters in the POST url. If you refer to the code I pasted for you, you can see that the parameters is for GET request only. The POST request receive a BidViewModel and NOT the parameters... So once all this is clear, it's currently the POST request that it not accessible when I submit the form. I get a 404 page not found and I don't know why. - Karine

1 Answers

1
votes

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 
                });