Я хотел бы добавить части к продукту в моем веб-приложении, но, похоже, это работает, только если оно жестко закодировано. Я поместил тот же код в функцию, и все, кажется, работает (в консоли), но при созданиипродукт, который детали не будут отображать на странице сведений. Значения не будут привязаны к списку ..
разрыв строки в случае, если (Modelstate.IsValid) показывает: Parts null
create controller
// POST: Products/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(AddProductViewModel viewModel)
{
if (ModelState.IsValid)
{
_context.Add(viewModel.Product);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View();
}
Создать вид продукта
@model IctWizard.ViewModel.AddProductViewModel
@{
ViewData["Title"] = "Create";
}
<script>
var i = 0;
jQuery('document').ready(function ($) {
$('#plus').click(function () {
inputValues = $('#partInput').val();
content = $('#partInput :selected').text();
console.log(content);
$("#parts").find("tbody").append("<tr><td><input asp-for=\"Product.ProductParts[" + i + "].PartId\" value=\"" + inputValues + "\" /></td></tr>");
console.log($("#parts").find("tbody"));
i++;
});
})
</script>
<h1>Create product</h1>
<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="Product.ProductName" class="control-label">Product name</label>
<input asp-for="Product.ProductName" class="form-control" />
<span asp-validation-for="Product.ProductName" class="text-danger"></span>
</div>
<div>
</div>
<div class="form-group">
<label asp-for="Product.ProductPrice" class="control-label">Product price</label>
<input asp-for="Product.ProductPrice" class="form-control" />
<span asp-validation-for="Product.ProductPrice" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Product.ReleaseDate" class="control-label">Release date</label>
<input asp-for="Product.ReleaseDate" class="form-control" />
<span asp-validation-for="Product.ReleaseDate" class="text-danger"></span>
</div>
<div class="form-group">
<label class="control-label"></label>
<select name="Part" id="partInput">
@foreach (var part in Model.Parts)
{
<option value="@part.Id">@part.Description</option>
}
</select>
<div class="btn btn-primary" id="plus">Add part</div>
</div>
<div class="row">
<div class="col-sm-12">
<table id="parts">
<thead>
<tr>
</tr>
</thead>
<tbody><tr><td>
@** <input type="hidden" asp-for="Product.ProductParts[0].PartId" value="7" />
<input type="hidden" asp-for="Product.ProductParts[0].PartId" value="6" />
<input type="hidden" asp-for="Product.ProductParts[1].PartId" value="7" />
<input type="hidden" asp-for="Product.ProductParts[2].PartId" value="8" />
<input type="hidden" asp-for="Product.ProductParts[0].Quantity" value="2" />
<input type="hidden" asp-for="Product.ProductParts[1].Quantity" value="5" />
<input type="hidden" asp-for="Product.ProductParts[2].Quantity" value="9" />*@
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="form-group">
<hr/>
<input type="submit" value="Create product" class="btn btn-primary" style="margin-top: 10px"/>
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Модель продукта
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace IctWizard.Models
{
public class Product
{
public int Id { get; set; }
public string ProductName { get; set; }
public int ProductPrice { get; set; }
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
public IList<ProductPart> ProductParts { get; set; }
}
}
ProductPart using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace IctWizard.Models
{
public class ProductPart
{
public int ProductId { get; set; }
public int PartId { get; set; }
public Product Product { get; set; }
public Part Part { get; set; }
[Required]
public int Quantity { get; set; }
}
}
Модель детали using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.IO;
using System.Text;
namespace IctWizard.Models
{
public class Part
{
public int Id { get; set; }
[Required]
public string Description { get; set; }
public IList<SupplierPart> SupplierParts { get; set; }
public IList<ProductPart> ProductParts { get; set; }
}
}
AddProductViewModel using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IctWizard.Models;
namespace IctWizard.ViewModel
{
public class AddProductViewModel
{
public Product Product { get; set; }
public IList<Part> Parts { get; set; }
}
}