2016-11-24 76 views
3

有人可以幫我解決這個問題。我試圖限制張貼綁定參數的行爲,但它似乎根本不起作用。當我刪除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">[email protected]</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");} 
} 

如果我有問題,或者它只是一個已知的asp.net核心問題,有人可以指示pelase嗎?

+1

視圖模型不應該包含屬於數據模型的屬性。您查看模型應該包含屬性'ID','CategoryId','Description'等 - 即只是想在視圖中需要。當正確使用視圖模型時,從不需要'[Bind]'屬性 –

+3

也不需要爲每個屬性添加前綴,只需使用:[Bind(「Id」,「CategoryId」,「Description」, 「Title」,Prefix = nameof(ProductCreateViewModel.Product))]' –

+0

@StephenMuecke:我遵循MS原創文章DRY(不要重複自己)的原則。但發現文件上傳的問題,因爲它沒有包含在帖子中,所以我添加了一個VM和Model和IFromFile集合。欲瞭解更多信息,請訪問這裏的帖子:https://docs.microsoft。com/en-us/aspnet/core/tutorials/first-mvc-app/controller-methods-views – NiL

回答

1

您需要將您的值作爲params string[],而不是用逗號分隔的一個string

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

Source

+0

他們都能正常工作。不管怎樣,謝謝你,* Prefix = nameof(ProductCreateViewModel.Product)* param! – NiL

2

爲什麼你使用Bind爲你的情況我不太清楚。

只需創建您需要的屬性的門衛ViewModelProductCreateStort

然後在您的控制器簽名中使用此ViewModel並繼承您的主模型。

這樣你就不會亂Bind和減少對POST

3

您的PARAMS雖然我是相當新的ASP.NET核心我自己(和未來的這個問題7月中下旬),我就遇到了這個同樣的問題。我認爲這裏的關鍵是你必須綁定「產品」才能被考慮。綁定「Product.Id」本身看起來不夠好。所以這應該工作:

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

當然,哈米德Mosalla的評論是一個更好的選擇,如果你所有的綁定屬性是嵌套對象(這會導致不知道爲什麼你需要擺在首位視圖模型)上。在我的情況下,我有一個嵌套對象和一個本地屬性,所以使用「前綴」解決方案並不是正確的做法。

無論如何,希望這可以幫助別人。