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
  • 511
  • 7
  • 21
  • 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

0 Answers0