繁体   English   中英

asp.net核心mvc表单上的viewmodel为空

[英]asp.net core mvc form viewmodel empty on post

当我单击按钮提交表单时,viewModel没有被绑定。 我不确定这是否与作为数组的模型属性有关。

调节器

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

            return View(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>

不要使用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... -->
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM