0

I need to use form inside table cell with fields in another cells. I'm using input form attribute. MVC passing data to controller works correctly but validation before form send doesn't work with this attribute.

<tr>
<td>
    @Html.EditorFor(model => item.Code, new { htmlAttributes = new { @class = "form-control inline-edit", form = "editForm" + item.Id } })
    @Html.ValidationMessageFor(model => item.Code)
</td>
<td>
    @Html.EditorFor(model => item.Name, new { htmlAttributes = new { @class = "form-control inline-edit", form = "editForm" + item.Id } })
    @Html.ValidationMessageFor(model => item.Name)
</td>
<td class="text-right">
    @using (Html.BeginForm("UpdatePrintMaterial", "Production", FormMethod.Post, new { id = "editForm" + item.Id }))
    {
        @Html.AntiForgeryToken()
        @Html.HiddenFor(model => item.Id)
        <button class="btn btn-default save" data-toggle="tooltip" title="Zapisz"><i class="fa fa-save"></i></button>
    }
</td></tr>

How to workaroud this issue to use validation before sending data?

bozydarlelutko
  • 451
  • 6
  • 20
  • 1
    Short answer is no. MVC's client side validation (using `jquery.validate.unobtrusive.js`and `jquery.validate.js` only works for form controls inside a `
    ` element. It would be better to use layout elements (positioning, floats etc) to mimic a table, rather than a `` element.
    –  Mar 26 '18 at 11:23
  • However, having multiple forms (one for each row) makes no sense - you can only submit one form at a time. If you want to be able to make edits on multiple rows, then have a single form and generate the view correctly using a `for` loop or `EditorTemplate` - currently your code wont even work correctly because your name attributes do not match your model) - refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943). –  Mar 26 '18 at 11:24
  • Alternatively, have an edit link in each row and display a dialog form for editing a row. –  Mar 26 '18 at 11:24

1 Answers1

0

Revised : As per your requirenment,

You can have 3 separate form tags, one for each button. Just make sure you have an input field inside the form for the data you want to pass. For example if your action method is accepting the EmployeeId with a a parameter called EmployeeId, you should have in input hidden field with the same inside the form.

@model IEnumerable<Employee>
<table>
@foreach(var item in Model)
{
<tr>
   <td>@item.EmployeeName</td>
   <td>@item.EmployeeGender</td>
   <td>@item.EmployeeCity</td>
   <td>@item.EmployeeDateOfBirth</td>
   <td>
     @using(Html.BeginForm("Details","YourControllerName"))
     {
       <input type="hidden" name="EmployeeId" value="@item.EmployeeId" />
       <input type="submit" value="Details" />
     }
     @using(Html.BeginForm("Edit","YourControllerName"))
     {
       <input type="hidden" name="EmployeeId" value="@item.EmployeeId" />
       <input type="submit" value="Edit" />
     }
     @using(Html.BeginForm("Delete","YourControllerName"))
     {
       <input type="hidden" name="EmployeeId" value="@item.EmployeeId" />
       <input type="submit" value="Delete" />
     }
   </td>
   </tr>
   }

* Also remember, nested forms are invalid HTML. So make sure you do not have those.

Aftab Lala
  • 95
  • 5