加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-core – 为什么Bind属性似乎破坏了我对嵌套对象的模型绑

发布时间:2020-12-16 06:50:35 所属栏目:asp.Net 来源:网络整理
导读:有人可以帮我解决这个问题.我试图限制使用绑定参数动作的发布,但它似乎根本不起作用.当我删除Bind关键字时,一切都开始成为一个魅力. 这是代码示例: 查看型号: public class ProductCreateViewModel{ public Product Product { get; set; } public ICollect
有人可以帮我解决这个问题.我试图限制使用绑定参数动作的发布,但它似乎根本不起作用.当我删除Bind关键字时,一切都开始成为一个魅力.

这是代码示例:

查看型号:

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

行动:

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

视图:

@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");}
}

有人pelase表明我是否有问题或者它只是一个已知的asp.net核心问题?

解决方法

虽然我自己对ASP.NET Core很新(并且在7个月后才讨论这个问题),但我遇到了同样的问题.我认为这里的关键是你必须绑定“产品”才能被考虑.绑定“Product.Id”本身似乎不够好.所以这应该工作:

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

当然,如果所有绑定属性都在嵌套对象上,那么Hamid Mosalla的注释是更好的选择(这会让你想知道为什么你需要一个视图模型).在我的例子中,我有一个嵌套对象和一个本地属性,所以使用“前缀”解决方案不是正确的做法.

无论如何,希望这有助于某人.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读