I have a form whose front page loads up correctly, but whenever I try to submit, I get the following error:
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Search/DoSearch
I have an MVC 3 form with the following control
public class HomeController : Controller
{
[HttpGet]
public ViewResult Index()
{
return View();
}
[HttpPost]
public ViewResult Index(FormModel formModel)
{
return View("Thanks", formModel);
}
}
the Index page has the form
@model RequestForm.Models.FormModel
@{
Layout = null;
}
<html>
<head>
<link rel="Stylesheet" href="@Href("~/Content/Site.css")" type="text/css"/>
<title>Request page</title>
</head>
<body>
@using (Html.BeginForm("DoSearch", "Search", FormMethod.Post, new { @class = "form-class" })){
@Html.LabelFor(x => x.fullName,"Full Name")
@Html.TextBoxFor(x => x.fullName)
@Html.LabelFor(x => x.address, "Address")
@Html.TextBoxFor(x => x.address)
@Html.LabelFor(x => x.phone, "Phone Number")
@Html.TextBoxFor(x => x.phone)
@Html.LabelFor(x => x.email,"Email")
@Html.TextBoxFor(x => x.email)
<br />
<input type="submit" value="submit" />
}
</body>
</html>
I also have a thanks view page
@model RequestForm.Models.FormModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Thanks</title>
</head>
<body>
<div>
Thank you for your submission
</div>
</body>
</html>
why is the thanks view page not being called upon and why does the requested URL search/Dosearch?
Your form is being posted to the
DoSearchaction method ofSearchcontroller. You need to change the Form declaration part in your view to fix that. Change it toIndexaction method ofHomeController.In your view,
change this
to
If you don’t want to give the class attribute to your form, you can simplify the above like this