4

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

Paul S
  • 89
  • 12
  • 1
    Do you receive any errors on the client? Such as a 4xx or 5xx code? – Kevin Burdett Mar 14 '16 at 17:37
  • If the class that defines the controller is named `FolderAccessController`, you should be able to access it using `/FolderAccess/SubmitSelected` and `@Html.BeginForm("SubmitSelected", "FolderAccess", ...)` instead of `FolderAccessController` – haim770 Mar 14 '16 at 17:39
  • 1
    Also, why is the `enctype` set to `multipart/form-data` when you don't actually upload (nor expect) any file? – haim770 Mar 14 '16 at 17:43

2 Answers2

2

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"}))
Thiago Ferreira
  • 661
  • 3
  • 19
1

Thiago Ferreira and haim770 Thanks a lot! The solution is to use the combination of your comments. So:

@using (Html.BeginForm("SubmitSelected", "FolderAccess", FormMethod.Post))

at the Controller

Paul S
  • 89
  • 12
  • Perhaps you should consider accepting the correct answer by Thiago Ferreira rather than writing your own. –  Mar 14 '16 at 23:11