如何将ICollection对象添加到Create View Model MVC

时间:2017-05-22 19:02:37

标签: asp.net-mvc create-view

我的情景是: 我有一个名为“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; }

    }

问题是当我使用Entity Framework创建带有视图的控制器时,它允许我添加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>

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

感谢您的帮助。

1 个答案:

答案 0 :(得分: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>