0

我的场景是:我有一个名为“CarService”的类,如下所示:

 public class CarService 
    {

        public int CarId { get; set; }
        public Car Car { get; set; }

        public int ServiceId { get; set; }
        public Person Service { get; set; }

        public DateTime ServiceEntryDate { get; set; }

        public DateTime ServiceExitDate { get; set; }
        public ICollection <CarServiceAction> CarServiceActions { get; set; }
    }

这个类有一个集合“ ICollection <CarServiceAction> CarServiceActions

CarServiceActions 如下:

public class CarServiceAction
    {
        public int CarServiceId { get; set; }
        public CarService CarService { get; set; }
        public float Value { get; set; }

    }

问题是当我使用实体框架创建带有视图的控制器时,它允许我添加 CarService 的所有字段,但没有任何数据输入表单用于收集ICollection <CarServiceAction> CarServiceActions

这是我从控制器生成的创建视图:

@model RentaCar.Models.Instances.CarService 
@{
    Layout = null;
    ViewData["Title"] = " ";
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Create</title>
</head>
<body>

    <form asp-action="Create">
        <div class="form-horizontal">
            <h4>CarService</h4>
            <hr />
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="CarId" class="col-md-2 control-label"></label>
                <div class="col-md-10">
                    <select asp-for="CarId" class="form-control" asp-items="ViewBag.CarId"></select>
                </div>
            </div>
            <div class="form-group">
                <label asp-for="ServiceId" class="col-md-2 control-label"></label>
                <div class="col-md-10">
                    <select asp-for="ServiceId" class="form-control" asp-items="ViewBag.ServiceId"></select>
                </div>
            </div>
            <div class="form-group">
                <label asp-for="ServiceEntryDate" class="col-md-2 control-label"></label>
                <div class="col-md-10">
                    <input asp-for="ServiceEntryDate" class="form-control" />
                    <span asp-validation-for="ServiceEntryDate" class="text-danger"></span>
                </div>
            </div>
            <div class="form-group">
                <label asp-for="ServiceExitDate" class="col-md-2 control-label"></label>
                <div class="col-md-10">
                    <input asp-for="ServiceExitDate" class="form-control" />
                    <span asp-validation-for="ServiceExitDate" class="text-danger"></span>
                </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>

        </div>
    </form>
</body>
</html>

问题是如何通过单击“添加详细信息”按钮添加 CarServiceAction 的详细信息。

谢谢你的帮助。

4

1 回答 1

0

事实上,我想成为这样的人:

    <div class="form-group">
                    <label asp-for="CarService.CarServiceAction.CarServiceId" class="col-md-2 control-label"></label>
                    <div class="col-md-10">
                        <input asp-for="CarService.CarServiceAction.CarServiceId" class="form-control" />
                    </div>
                </div>
                <div class="form-group">
                    <label asp-for="ServiceEntryDate" class="col-md-2 control-label"></label>
                    <div class="col-md-10">
                        <input asp-for="CarService.CarServiceAction.Value" class="form-control" />
                        <span asp-validation-for="CarService.CarServiceAction.Value" class="text-danger"></span>
                    </div>
<input   value="add new service action" class="btn btn-default" />
                </div>
于 2017-05-23T09:24:00.503 回答