asp.net核心MVC表单视图模型在发布后为空

时间:2019-05-10 09:14:14

标签: c# asp.net-mvc asp.net-core

当我单击按钮提交表单时,viewModel没有绑定。我不确定这是否与model属性是一个数组有关。

控制器

[HttpPost]
        public async Task<IActionResult> CommodityAdditionalCodes(CommodityAdditionalCodesViewModel viewModel)
        {
            //todo code in here which calls webservice 

            return View(viewModel);
    }

ViewModel

 public class CommodityAdditionalCodesViewModel
    {
        public CommodityAdditionalCodeType[] Types { get; set; }
    }

查看

@model Asm.Helios.ToolboxMvc.ViewModels.CommodityAdditionalCodesViewModel

@{
    ViewData["Title"] = "CommodityAdditionalCodes";
}

<Section>

    <button type="button" id="btnCommit">Commit Changes</button><span id="isDirtyMessage" class="warning" style="display:none">There are unsaved changes, press the commit changes button to save them</span>
</Section>

<form id="frmAdditionalCodes" name="frmAdditionalCodes"  method="post" >
<table class="table header-fixed">
    <thead>
        <tr >
            <th>Origin</th>
            <th>Description</th>
            <th>Valid From</th>
            <th>Valid To</th>
            <th>Type</th>
            <th>Customs Identifier</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.Types)
        {
            <tr>
                <td>
                    <span>@item.Origin</span>
                    <input type="text" value="@item.Origin" style="display:none"/>
                </td>
                <td>
                    <span>@item.Description</span>
                    <input type="text" value="@item.Description" style="display:none" />
                </td>
                <td>
                    <span>@item.ValidFrom</span>
                    <input type="text" value="@item.ValidFrom" style="display:none" />
                </td>
                <td>
                    <span>@item.ValidTo</span>
                    <input type="text" value="@item.ValidTo" style="display:none" />
                </td>
                <td>
                    <span>@item.Type</span>
                    <input type="text" value="@item.Type" style="display:none" />
                </td>
                <td>

                    <span>@item.CustomsIdentifier</span>
                    <!--<input type="text" value="@item.CustomsIdentifier" style="display:none" />-->
                    @Html.TextBoxFor(m=>item.CustomsIdentifier, new {@style="display:none"})</td>
                <td>
                    <button type="button" id="deleteItem">Delete</button>
                    <button type="button" id="editItem">Edit</button>
                    <button type="button" id="updateItem" style="display:none">Update</button>
                    <button type="button" id="cancelItem" style="display:none">Cancel</button>
                </td>
            </tr>
        }

    </tbody>
</table>
</form>

1 个答案:

答案 0 :(得分:3)

不要使用foreach循环,而要使用经典的for并为模型建立索引,例如Model.Types[index]

另外,您应该考虑使用标签帮助程序,它使Razor代码更容易阅读,因为它与真实HTML更加相似。

@for (var index = 0; index < Model.Types.Count; index++)
{
    <input type="text" asp-for="Model.Types[index].Origin" />

    <!-- etc... -->
}