简体   繁体   English

ASP.Net Core MVC - 复杂的 ViewModel 没有将属性与对象列表绑定

[英]ASP.Net Core MVC - Complex ViewModel not binding property with list of objects

I have a ViewModel with some properties and a list of objects.我有一个带有一些属性和对象列表的 ViewModel。 I also have a controller to create the data from the ViewModel.我还有一个 controller 从 ViewModel 创建数据。

Whenever the request gets back to the controller, I restrict the binding on Nickname and Items .每当请求返回到 controller 时,我都会限制对NicknameItems的绑定。 The ModelState is invalid as the binding on the list is null . ModelState无效,因为列表上的绑定是null

ViewModel视图模型

public class ContainerViewModel
{
    public Guid ID { get; set; }
    public string Nickname { get; set; }
    public List<ItemViewModel> Items { get; set; }
    public Guid NewItemID { get; set; }
    public string NewItemNickname { get; set; }
    public uint NewItemQuantity { get; set; }

    public void AddNewItem()
    {
        Items.Add(new ItemViewModel {
            ID = NewItemID,
            Nickname = NewItemNickname,
            Quantity = NewItemQuantity
        });
        NewItemID = Guid.NewGuid();
        NewItemNickname = default;
        NewItemQuantity = default;
    }

    public class ItemViewModel
    {
        public Guid ID { get; set; }
        public string Nickname { get; set; }
        public uint Quantity { get; set; }
    }
}

Controller Controller

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Nickname,Items")] ContainerViewModel containerVM, CancellationToken token)
{
    if (ModelState.IsValid) {
        await _manager.Add(PrepareDataModel(containerVM), token);
        return RedirectToAction(nameof(Index));
     }
     return View(containerVM);
}

--EDIT-- - 编辑 -

Create.cshtml创建.cshtml

@model MyProject.Models.ContainerViewModel

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

<h1>Create</h1>

<h4>Container</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Nickname" class="control-label"></label>
                <input asp-for="Nickname" class="form-control" />
                <span asp-validation-for="Nickname" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Quantity" class="control-label"></label>
                <input asp-for="Quantity" type="number" class="form-control" />
                <span asp-validation-for="Quantity" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Items" class="control-label"></label>
                @await Html.PartialAsync("Items/_Create")
                <span asp-validation-for="Items" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

Items/_Create.cshtml项目/_Create.cshtml

@model MyProject.Models.ContainerViewModel

<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.NewItemNickname)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.NewItemQuantity)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model.Items) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Nickname)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Quantity)
            </td>
            <td></td>
        </tr>
}
        <tr>
            <td>
                <input asp-for="NewItemNickname" class="form-control" />
                <span asp-validation-for="NewItemNickname" class="text-danger"></span>
            </td>
            <td>
                <input asp-for="NewItemQuantity" type="number" class="form-control" />
                <span asp-validation-for="NewItemQuantity" class="text-danger"></span>
            </td>
            <td>
                <input type="submit" asp-action="CreateNewItem" value="Add" class="btn btn-secondary" />
            </td>
        </tr>
    </tbody>
</table>

In your Partial View, you just use @Html.DisplayFor(xxxxx) to show the data of Items ,So these data will not be submit.在你的 Partial View 中,你只是使用@Html.DisplayFor(xxxxx)来显示Items的数据,所以这些数据不会被提交。 Change your code like this:像这样更改您的代码:

Items/_Create.cshtml项目/_Create.cshtml

//.......
@for (var i = 0; i < @Model.Items.Count; i++)
{
        <tr>
            <td>
                @Html.DisplayFor(modelItem => @Model.Items[i].Nickname)

                // use hidden to submit the data
                <input type="hidden" asp-for="@Model.Items[i].Nickname">
            </td>
            <td>
                @Html.DisplayFor(modelItem => @Model.Items[i].Quantity)
                <input type="hidden" asp-for="@Model.Items[i].Quantity">
            </td>
            <td></td>
        </tr>
 }
//.......

Result结果

Because you don't provide the code of CreateNewItem , I think it is may be use d to add new data in Items , I don't write this action, So ss .因为你没有提供CreateNewItem的代码,我觉得可能是用来往Items添加新数据的,这个动作我就不写了,所以ss 3 will not be submited to controller. 3不会提交到controller。

在此处输入图像描述

在此处输入图像描述

You can see controller can receive Items successfully.可以看到controller可以成功收到Items

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

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