Reputation: 1516
In the application I am working on, I have an Html page inside views folder and I have mentioned the action as follows.
<form name="form" onsubmit="return validateForm();" method="post" action="//Controllers/RegistrationController.cs">
The registration controller returns a view.
public ActionResult Detail(string name)
{
return View();
}
When I run the program, I get server not found error.
I also tried changing the action string to action="//Controllers/RegistrationController.cs/Detail"
but got the same error.
Does the action string have to be written in some other way?
Thank you very much in advance for your help.
Upvotes: 0
Views: 4082
Reputation: 60466
You don't have to set the path like in your solution. You don't need to set Controllers
because the framework knows that you mean the controller.
Assuming that you dont change the routing in global.asax
, your RegistrationController.cs
has an ActionMethod called Detail
(decorated with [HttpPost]
) and the following folder structure within your project.
Views/Registration/Detail.cshtml
@using (Html.BeginForm("Detail", "Registration", FormMethod.Post, new { @onSubmit = "return validateForm();" }))
{
// Your Form's content
}
Upvotes: 1
Reputation: 1038810
Assuming you are using the default routes ({controller}/{action}/{id}
) you need:
action="/Registration/Detail"
Actually I would recommend you using HTML helpers to generate forms and never hardcode them as you did:
@using (Html.BeginForm("Details", "Registration", FormMethod.Post, new { name = "form", onsubmit = "return validateForm();" }))
{
...
}
Upvotes: 2
Reputation: 532465
/registration/detail
- you don't need to reference the path to the actual file. The framework finds the controller class and invokes the requested action for you. It uses the routes as defined in global.asax.cs
to determine the controller and action from the url. The default route is {controller}/{action}/{id}
where the first two have defaults of "Home" and "Index", respectively, and the third is optional. You can change this if you want by adding/modifying the route set up.
Upvotes: 1