Bind attribute will not work with nested objects

Help me solve this problem. I am trying to limit sending using the binding parameter action, but it seems to not work at all. When I deleted the Bind keyword, everything began to work like a charm.

Here is a sample code:

Show model:

public class ProductCreateViewModel
{
    public Product Product { get; set; }
    public ICollection<IFormFile> Images { get; set; }
}

Act:

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Product.Id,Product.CategoryId,Product.Description,Product.Title")] ProductCreateViewModel productVM)
        {
            if (ModelState.IsValid)
            {
                _context.Add(productVM.Product);
                await _context.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewData["CategoryId"] = new SelectList(_context.Categories.Include(c => c.Categories).Where(c => c.ParentCategoryId == null), "Id", "Name", productVM.Product.CategoryId);
            return View(productVM);
        }

View:

@model CatalogWebApp.Models.ProductsViewModels.ProductCreateViewModel

@{
    ViewData["Title"] = "Add Product";
    ViewData["BigPageTitle"] = "Products";
    ViewData["PageBoxTitle"] = "Add New Product";
}

<form asp-action="Create">
    <div class="form-horizontal">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="Product.CategoryId" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <select name="Product.CategoryId" class ="form-control">
                    @foreach(Category item in (ViewBag.CategoryId as SelectList).Items)
                    {
                        <option value="@item.Id">@item.Name</option>
                        if (item.Categories != null && item.Categories.Count > 0)
                        {
                            foreach (var subCat in item.Categories)
                            {
                                <option value="@subCat.Id">--@subCat.Name</option>
                            }
                        }
                    }
                </select>
            </div>
        </div>
        <div class="form-group">
            <label asp-for="Product.Description" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="Product.Description" class="form-control" />
                <span asp-validation-for="Product.Description" class="text-danger" />
            </div>
        </div>
        <div class="form-group">
            <label asp-for="Product.Title" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="Product.Title" class="form-control" />
                <span asp-validation-for="Product.Title" class="text-danger" />
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
</form>

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

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

Can someone whitewash indicate if I have a problem, or is this just a known asp.net core problem?

+4
source share
3 answers

ASP.NET Core ( 7 ), . , , "", . "Product.Id" . :

[Bind("Product,Product.Id,Product.CategoryId,Product.Description,Product.Title")]

, Hamid Mosalla - , ( , ). AND , "" .

, , -.

+3

, Bind .

sepatate ViewModel , ProductCreateStort.

ViewModel .

, Bind POST

+2

You need to pass your values ​​as params string[], and not as one string, separated by commas:

[Bind("Product.Id","Product.CategoryId","Product.Description","Product.Title")]

See Source

+1
source

Source: https://habr.com/ru/post/1661809/


All Articles