3

When I click the button to submit my form, the viewModel is not being bound. I'm not sure whether this is to do with the model property being an array.

Controller

[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; }
    }

View

@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>
4
  • 1
    Don't use a foreach loop, instead use a classic for and index the model, for example Model.Types[index]. Also you should use tag helpers, in this case something like <input type="text" asp-for="Model.Types[index].Origin" /> Commented May 10, 2019 at 9:37
  • Oh, and are you using style display: none so user never sees the inputs? If so, instead use <input type="hidden" ...> Commented May 10, 2019 at 9:38
  • Ah cheers - yes this rings a bell I will try this Commented May 10, 2019 at 9:39
  • Thanks DavidG that was it exactly -post as an answer and I will accept it Commented May 10, 2019 at 9:49

1 Answer 1

3

Don't use a foreach loop, instead use a classic for and index the model, for example Model.Types[index].

Also you should consider using tag helpers, it makes your Razor code much easier to read as it more closely resembles real HTML.

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

    <!-- etc... -->
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.