MVC action does not start in controller

I made a model, some fields and a button in the view:

View:

@model IEnumerable<EnrollSys.Employee>
 @foreach (var item in Model)
    {
      @Html.TextBoxFor(modelItem => modelItem.name)
    }
<input type="submit" value="Save" class="btn btn-default" style="width: 20%" />

Controller:

   public ActionResult Index()
        {
            var model = selectModels();
            return View(model);
        }

        [HttpPost]
        public ActionResult Save(IEnumerable<EnrollSys.Employee> model)
        {
            return View();
        }

The problem is this:

Why is the Save action not running?

+1
source share
1 answer

You need an item <form>to submit your controls. In your case, you need to specify the name of the action, because its not the same as the thet method generated the view ( Index())

@using (Html.BeginForm("Save"))
{
   .... // your controls and submit button
}

Save(), , foreach name , , ( html - id).

for ( IList) EditorTemplate Employee.

for

@model IList<EnrollSys.Employee>
@using (Html.BeginForm("Save"))
{
  for (int i = 0; i < Model.Count; i++)
  {
    @Html.TextBoxFor(m => m[i].name)
  }
  <input type="submit" value="Save" class="btn btn-default" style="width: 20%" />
}

EditorTemplate

/Views/Shared/EditorTemplates/Employee.cshtml

@model EnrollSys.Employee
@Html.TextBoxFor(m => m.name)

@model IEnumerable<EnrollSys.Employee> // can be IEnumerable
@using (Html.BeginForm("Save"))
{
  @Html.EditorFor(m => m)
  <input type="submit" value="Save" class="btn btn-default" style="width: 20%" />
}
+3

Source: https://habr.com/ru/post/1589422/


All Articles