2017-05-22 2 views
0

Mon scénario est le suivant: J'ai une classe appelée "CarService" comme ci-dessous:Comment ajouter objet ICollection créer Voir le modèle MVC

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; } 
    } 

cette classe a une collection "ICollection <CarServiceAction> CarServiceActions"

les CarServiceActions est comme ci-dessous:

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

    } 

le problème est quand je crée le contrôleur avec des vues en utilisant Entity Framework, il me permet d'ajouter tous les champs de CarServ la glace, mais il n'y a pas toute forme de saisie de données pour la collecte ICollection <CarServiceAction> CarServiceActions

Ceci est ma création vue générée par le contrôleur:

@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> 

La question est de savoir comment puis-je ajouter des détails de CarServiceAction par cliquer sur un bouton « adddetail ».

Merci pour votre aide.

+0

'@model ICollection '? – nurdyguy

+0

Ceci est un formulaire de création pour CarService qui contient une liste de CarServiceAction – user733256

+0

Quelles sont les données exactes à transmettre à partir du contrôleur? – nurdyguy

Répondre

0

En fait, je veux être quelque chose comme ceci:

<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>