В моем проекте у меня есть форма, и одно из ее полей - таблица. Однако, когда я отправляю свою форму, в моем контроллере моя соответствующая переменная для этой таблицы равна нулю, даже я ввожу некоторые данные. Как я могу это решить? И в чем проблема?
Мой главный взгляд:
<div class="editor-label">
@Html.LabelFor(model => model.Car)
</div>
<div class="editor-field">
<p>
@{Html.RenderPartial("FormTabs/FormLists/ListCars",Model);}
</p>
</div>
//other code
Мой табличный вид (частичный "FormTabs/FormLists/ListCars"):
@using MyProject.Resources
@model MyProject.ViewModels.Car.CarViewModel
@{const string tableId = "tableCars";}
<table class="table" id="@(tableId)">
<thead>
<tr>
<th>
@MyResources.Header1
</th>
<th>
@MyResources.Header2
</th>
<th colspan="3">
@MyResources.Header3
</th>
</tr>
</thead>
<tbody>
@Html.DisplayFor(model => model.Car)
</tbody>
</table>
<div id="initialButtons" data-tableId="@tableId" style="display: block">
<p>
<button id="btnAdd" data-tableId="@tableId" type="button" class="enable" onclick="addItem('@(tableId)', '../NewCarRow/')">
@MyResources.BtAdicionar</button>
<button type="button" data-tableId="@tableId" class="disable" id="btEdit" onclick="editItem('@(tableId)')">
@MyResources.BtEditar</button>
<button type="button" data-tableId="@tableId" class="disable" id="btDelete" onclick="deleteItem('@(tableId)')">
@MyResources.BtRemover</button>
</p>
</div>
<div id="btnsEditing" data-tableId="@tableId" style="display: none">
<p>
<button id="btSaveEdition" data-tableId="@tableId" type="button" class="enable" onclick="saveUpdates('@(tableId)')">
@MyResources.BtGravar</button>
<button id="btCancelEdition" data-tableId="@tableId" type="button" class="enable" onclick="cancelEdition('@(tableId)')">
@MyResources.BtCancelar</button>
</p>
</div>
Мой DisplayTemplate:
@model MyProject.Model.Cars.Car
@if (Model != null)
{
<tr id='@(Model.Id)' onclick="selectRow('tableCars', '@(Model.Id)')">
@Html.HiddenFor(model => model.Id)
<td>
@Html.TextBoxFor(model => model.Name, new { style = "display:none" })
@Html.ValidationMessageFor(model => model.Name)
@Html.DisplayFor(model => model.Name)
</td>
<td>
@Html.TextBoxFor(model => model.Owner, new { style = "display:none" })
@Html.ValidationMessageFor(model => model.Owner)
@Html.DisplayFor(model => model.Owner)
</td>
</tr>
}
Мой новый вид строки:
@model MyProject.Model.Cars.Car
<tr id='@(Model.Id)' onclick="selectRow('tableCars', '@(Model.Id)')">
@Html.HiddenFor(model => model.Id)
<td>
@Html.TextBoxFor(model => model.Name, new { @class = "iEdit" })
@Html.ValidationMessageFor(model => model.Name)
</td>
<td>
@Html.TextBoxFor(model => model.Owner, new { @class = "iEdit" })
@Html.ValidationMessageFor(model => model.Owner)
</td>
</tr>
Наконец, я получил решение: для каждого "для" помощника у меня есть "My new row view" и "My DisplayTemplate", мне нужно указать имя и поле id. Например:
@model MyProject.Model.Cars.Car
@{
string modelFieldInitialName = "Car[" + Model.Id + "].";
string modelFieldInitialId = "Car" + Model.Id + "_";
}
<tr id='@(Model.Id)' onclick="selectRow('tableCars', '@(Model.Id)')">
@Html.HiddenFor(model => model.Id, new { Name = modelFieldInitialName + "Id", id = modelFieldInitialId + "Id" })
<td>
@Html.TextBoxFor(model => model.Name, new { @class = "iEdit" })
@Html.ValidationMessageFor(model => model.Name, new { Name = modelFieldInitialName + "Name", id = modelFieldInitialId + "Name" })
</td>
<td>
@Html.TextBoxFor(model => model.Owner, new { @class = "iEdit" })
@Html.ValidationMessageFor(model => model.Owner, new { Name = modelFieldInitialName + "Owner", id = modelFieldInitialId + "Owner" })
</td>
</tr>
и при этом код может отображать вставленные значения в правую переменную и отправлять их