0

I am working on a sample mvc project in which I am trying to post data to a controller. I have posted sample (see below), but if I add [HttpPost] to method I am getting '404' error.

View:

<% using (Html.BeginForm()) { %> <%= Html.Telerik().NumericTextBox() .Name("NumericTextBox") .Spinners(false) .EmptyMessage("ID") %> <input type="submit" value="Submit" /> <% } %> 

Controller:

[HttpPost] public ActionResult GetDetails(int id) { return View(); } **I also tried,** [HttpPost] public ActionResult GetDetails(FormCollection collection) { return View(); } 

Route:

routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Customer", action = "GetDetails", id = UrlParameter.Optional } // Parameter defaults ); 
1
  • 1
    Its quite typical to find where the problem is. Could you please paste the whole code of your view and controller? Commented Sep 21, 2012 at 16:34

2 Answers 2

2

You'll want the Name to match the parameter on the controller, so I believe it should be like this:

Html.Telerik().NumericTextBox() .Name("id") 

Notes:

  1. Although you specified UrlParameter.Optional on the id route parameter, it's not truly optional unless you make it nullable (ie, int? id) in the controller action.
  2. Normally you should use GET and not POST for HTTP requests that don't change anything on the server, which seems to be the case here.
Sign up to request clarification or add additional context in comments.

4 Comments

He's doing a test. In a real app he would be changing the value, so I think it's just confusing to tell him to use Get. Also, using id for the textbox will conflict with the routed values if a different id is passed on the url.
I am still getting the same error after matching name of textbox with parameter.
@CoolArchTek I don't see any other obvious problems. To confirm: your view above is GetDetails, is it?
Yes. but now it started working once I add another overloaded method without any parameters. public ActionResult GetDetails() { return View(); }
0

You should use the second method, but instead of FormsCollection, use:

GetDetails(int NumericTextBox)

The parameter must be the same name as the input box.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.