0

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

Community
  • 1
  • 1
Nico Pizzo
  • 105
  • 10

2 Answers2

0

The problem is very simple. If you submit to a Search view, and return the result in the same Search view, MVC interprets this as an invalid model state. That is the reason nothing happens. Even if you put:

if (ModelState.IsValid) {
    return View("Search", searchModel);
}

you will notice that the ModelState is valid but nothing will happen. In order to fix the issue either create a SearchResult action that receives the Post or do RedirectToAction("Search", searchModel)

This should solve the problem.

Huske
  • 9,186
  • 2
  • 36
  • 53
  • The problem is that I am not that I am getting a blank screen, it is that the Search method is not being hit when I set a breakpoint in the method. So I dont think anything is actually being returned back to the view. – Nico Pizzo Dec 03 '15 at 22:12
  • Are the view and the action located in the Area1\SearchController\Search? – Huske Dec 03 '15 at 22:15
  • That is correct. Iv updated the question above with routing information – Nico Pizzo Dec 03 '15 at 22:23
  • Try adding controller to the defaults in the MapRoute method. – Huske Dec 03 '15 at 22:25
0

Sorry I'm new member of stackoverflow and have no enough points to leave a comment so I try answer your question here. May be you can try this to see whether it executes that method or not:

public ActionResult Search(FormCollection form)

the you can access what is passed in using

string a = form["yourTextboxID"].ToString();
erntay2
  • 140
  • 3
  • 16