【问题标题】:asp.net core mvc form viewmodel empty on postasp.net 核心 mvc 表单视图模型在帖子上为空
【发布时间】:2019-05-10 09:14:14
【问题描述】:

当我点击按钮提交表单时,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]。你也应该使用标签助手,在这种情况下像&lt;input type="text" asp-for="Model.Types[index].Origin" /&gt;
  • 哦,您是否使用display: none 样式,所以用户永远看不到输入?如果是这样,请改用&lt;input type="hidden" ...&gt;
  • 啊,干杯 - 是的,这敲响了警钟,我会试试这个
  • 感谢 DavidG,这正是它的答案——我会接受它

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    相关资源
    最近更新 更多