So I am having trouble getting the POST Action in a controller to fire after submitting a form from a View. Here is some of the code that I have so far:
Model - SearchModel.cs
public class SearchModel
{
public string FormName { get; set; }
public string Category { get; set; }
public string EventType { get; set; }
public string UserId { get; set; }
}
Controller - SearchController.cs
[HttpPost]
public ActionResult Search(SearchModel searchModel) // This is not Firing
{
// other stuff
return View("Search", searchModel);
}
View - Search.cshtml
@model MainProject.Areas.Area1.Models.SearchModel
@{
ViewBag.Title = "Search";
}
@using (Html.BeginForm("Search", "Search", new { area = "Area1"}, FormMethod.Post, new { id = "fmSearch" }))
{
<div id="searchDiv">
<fieldset>
<legend>Search</legend>
<div>
<table style="padding-left: 110px">
<tr>
<td>
<label for="FormName">Form Name</label>
</td>
<td>
@Html.TextBoxFor(m => m.FormName, new { @id = "txtFormName", @class = "rounded formNumber uppercase", style = "width: 253px;", placeholder = "Form Name", maxlength = 12 })
</td>
</tr>
<tr>
<td>
<label for="Category">Category</label>
</td>
<td>
@Html.TextBoxFor(m => m.Category, new { @id = "txtCategory", @class = "rounded uppercase", style = "width: 253px;", placeholder = "Category", maxlength = 12 })
</td>
<td style="width: 100px"></td>
<td>
<label for="EventType">Event Type</label>
</td>
<td>
@Html.TextBoxFor(m => m.EventType, new { @id = "txtEventType", @class = "rounded uppercase", style = "width: 253px;", placeholder = "Event Type", maxlength = 12 })
</td>
</tr>
<tr>
<td>
<label for="UserId">User ID</label>
</td>
<td>
@Html.TextBoxFor(m => m.UserId, new { @id = "txtUserId", @class = "rounded uppercase", style = "width: 253px;", placeholder = "User ID", maxlength = 12 })
</td>
</tr>
<tr>
<td>
<input class="roundedbutton" type="submit" id="btnSearch" value="Search" name="button" title="Search" style="width:120px; margin-right: 15px; margin-top: 10px; margin-bottom: 5px;" />
</td>
</tr>
</table>
</div>
</fieldset>
</div>
}
When the submit button is click, it is expected that the View passes the model to the Controller Action "Search" but instead, I am returned a white screen.
As you can see in the code, I am in an Area called "Area1", but that should not make much of a difference. I would like to get this working like described instead of using an Ajax call.
I have looked at this but I have not had much success and it seems like what I have so far is correct but unfortunately it is not.
Any help getting the controller to fire on the button submit would be amazing.
EDIT: And when I say fire, I set a breakpoint in the Search method and the breakpoint is not hit.
EDIT2: This is the Map Routes from the Area1AreaRegistration.cs file that is generated when creating a new Area:
public class Area1AreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Area1";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
// tried with and without this
//context.MapRoute(
// name: "Search",
// url: "Area1/Search",
// defaults: new { controller = "Search", action = "Search", id = UrlParameter.Optional }
// );
context.MapRoute(
"Area1_default",
"Area1/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
It is also worth nothing that the model is located at ~/Areas/Area1/Models, the controller is located at ~/Areas/Area1/Controllers, and the View is located at ~/Areas/Area1/Views