Search code examples
asp.net-mvchtml-helperhtml.beginform

Html.BeginForm call the right Action in Controller


There are a lot of topics related to this question but I still did't figured out what I'm doing wrong.

I have a database where I manage access of different users to folders. On my View the User can select Employees which should have access to certain folder. Then I want to pass the selected Employees to Controller, where the database will be updated.

My Problem is: The right Action in the Controller class didn't get invoked.(I have a breakpoint inside)

Here is the View

@model DataAccessManager.Models.EmployeeSelectionViewModel
@{
    ViewBag.Title = "GiveAccessTo";
}

@using (Html.BeginForm("SubmitSelected", "FolderAccessController", FormMethod.Post, new { encType = "multipart/form-data"}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.fr_folder_uid_fk)
<div class="form-horizontal">
<input type="submit" value="Save" id="submit" class="btn btn-default" />

            <table id="tableP">
                <thead>
                    <tr>
                        <th>Selection</th>
                        <th>Second Name</th>
                        <th>First Name</th>
                        <th>Department</th>
                    </tr>
                </thead>
                <tbody id="people">
                    @Html.EditorFor(model => model.People)       
                </tbody>
            </table>

        </div>
    </div>
</div>
}

Here is the Controller reduced to the minimum

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SubmitSelected(EmployeeSelectionViewModel model)
{
    return View();
}

More Details: I am not sure what is causing the problem, so here some more details. The view is strongly typed to EmployeeSelectionViewModel, it represets the table with all Employees as a List here is the the code:

public class EmployeeSelectionViewModel
{
    public List<SelectEmployeeEditorViewModel> People { get; set; }
    public EmployeeSelectionViewModel()
    {
        this.People = new List<SelectEmployeeEditorViewModel>();
    }

    public Int64 fr_folder_uid_fk { get; set; }

    public IEnumerable<string> getSelectedIds()
    {
        // Return an Enumerable containing the Id's of the selected people:
        return (from p in this.People where p.Selected select p.fr_mavnr_fk).ToList();
    }
}

The SelectEmployeeEditorViewModel represents one row of the table with all Employees.

public class SelectEmployeeEditorViewModel
{
    public bool Selected { get; set; }

    public string fr_mavnr_fk { get; set; }
    public string firstName { get; set; }
    public string secondName { get; set; }
    public string dpt { get; set; }
}

And it has a View which create the checkboxes for each Employee

@model DataAccessManager.Models.SelectEmployeeEditorViewModel
<tr>
    <td style="text-align:center">
        @Html.CheckBoxFor(model => model.Selected)
        @Html.HiddenFor(model => model.fr_mavnr_fk)
    </td>
    <td>
        @Html.DisplayFor(model => model.secondName)
    </td>
    <td>
        @Html.DisplayFor(model => model.firstName)
    </td>
    <td>
        @Html.DisplayFor(model => model.dpt)
    </td>
</tr>

The /FolderAccessController/SubmitSelected URL is called in the browser when I press the Submit button, but as mentioned the Action isn't invoked.

EDIT: I get the HTTP 404 not found error after pressing the button


Solution

  • Try removing the "Controller" word from your Html.BeginForm() second parameter, it's not needed.

    @using (Html.BeginForm("SubmitSelected", "FolderAccess", FormMethod.Post, new { encType = "multipart/form-data"}))