2013-12-16 105 views
0

在我的项目中我有一个表单,其中一个字段是表。但是,当我提交表单时,在我的控制器中,即使输入了一些数据,我的模型对应变量也是空的。我该如何解决这个问题?问题是什么?表提交为空

我的主视图:

 <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> 
+0

只已经投入的助手将被调回控制器领域(除显示为) –

+0

@MattBodily通过jQuery中的函数我添加了“我的新行视图”代码“我的表视图”tbody。所以它应该是有效的。没有? – Ninita

+0

我将列表发送回控制器的唯一方法是使用ajax调用。我看到这里的人们把他们的所有领域都放到帮助者身上,比如我看到你们。如果我必须猜测你遇到的问题是字段在局部视图中。只是为了测试你应该把你的部分放入一个表单标签并提交表单来查看数据是否被返回 –

回答

0

最后,我得到了一个解决方案:对于每个“为”帮助我对“我的新行观点”和“我的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> 

以及与此,代码可以映射插入值,以正确的变量,并将其提交