使用EF Core在asp.net Core中从视图插入到多对多关系

时间:2019-01-06 10:30:56

标签: asp.net-core many-to-many entity-framework-core

因此,我在ASP.net Core应用程序中具有一个视图,该视图中添加了新的汽车维修,并且我还想在此模型中添加零件。该数据库有3个表:修理-零件-修理零件。

我设法创建了新的修复程序,但是我不知道如何将零件列表从该视图传递到控制器,并将其插入到RepairParts表中。

维修班

    public class Repair
    {
        [Key]
        public int RepairId { get; set; }

        public int VehicleId { get; set; }

        public string Notes { get; set; }

        public string Mileage { get; set; }

        public DateTime RepairDate { get; set; }

        public string uuid { get; set; }

        [ForeignKey("VehicleId")]
        public Vehicle Vehicle { get; set; }

        public virtual ICollection<RepairParts> Parts { get; set; }
    }

零件类

public class Part
{
    public int PartId { get; set; }
    public int Code { get; set; }
    public string PartCode { get; set; }
    public string Type { get; set; }
    public string Name { get; set; }
    public string Descr { get; set; }
    public string Manufacturer { get; set; }
    public string uuid { get; set; }

    public virtual ICollection<RepairParts> Repairs { get; set; }
}

维修零件类

public class RepairParts
{
    public int RepairId { get; set; }
    public virtual Repair Repair { get; set; }

    public int PartId { get; set; }
    public virtual Part Part { get; set; }

}   

维修控制器

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult 
    Create([Bind("VehicleId,Mileage,RepairDate,Notes")] RepairCreateModel repair)
    {
        try
        {
            Repair rep = new Repair();

            var NewRepair = _mapper.Map(repair, rep);

            _repairService.Add(NewRepair);


        }
        catch (Exception)
        {
            throw;

        }
        return Ok();
    }

维修服务

    public void Add(Repair repair)
    {
        string userid = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;

        repair.uuid = userid;
        _context.Repairs.Add(repair);
        _context.SaveChanges();
    }

修复创建视图

<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>

<input asp-for="VehicleId" value="@ViewBag.VehicleId" type="hidden" />

<div class="form-group row">
    <label class="col-sm-1 col-form-label">Owner</label>
    <div class="col-sm-2">
        <input type="text" class="form-control" value="@ViewBag.Owner" 
readonly>
    </div>
</div>

<div class="form-group row">
    <label class="col-sm-1 col-form-label">Vehicle</label>
    <div class="col-sm-2">
        <input type="text" class="form-control" value="@ViewBag.Vehicle" 
readonly>
    </div>

    <label class="col-sm-2 col-form-label">Licence Plates</label>
    <div class="col-sm-2">
        <input type="text" class="form-control" 
value="@ViewBag.LicencePlates" readonly>
    </div>
</div>
<div class="form-group row">

    <label class="col-sm-1 col-form-label">Mileage</label>
    <div class="col-sm-2">
        <input asp-for="Mileage" type="text" class="form-control">
    </div>

    <label class="col-sm-2 col-form-label">Date:</label>
    <div class="col-sm-2">
        <input asp-for="RepairDate" type="text" class="form-control">
    </div>
</div>

<div class="form-group row">

    <label class="col-sm-1 col-form-label">Notes</label>
    <div class="col-sm-8 row">
        <textarea asp-for="Notes" rows="5" class="form-control"></textarea>
    </div>
</div>


    <div class="form-group row">
        <input type="submit" value="Create" class="btn btn-primary" />
    </div>

</form>

任何人都知道我该如何实现?

0 个答案:

没有答案