2016-11-28 7 views
3

多対多の関係を使用しようとしていますが、私は顧客を編集して表示することに成功しましたが、私は作成できません。 私の例では、1人の顧客(クライアント)が1つ以上の社会に参加することができます。 Visual Studioを使用して段階的に、私の値が私の最初の「作成」機能に渡すことができないことに気付きました。私は正常にフィールドを取得したいが、POSTすることはできません。 マイClientController:ASP.netで多対多の関係を使用する

namespace CRM.ViewModels 
{ 
public class ClientViewModel 
{ 
    public Client Client { get; set; } 
    public IEnumerable<SelectListItem> AllSocietes { get; set; } 

    private List<int> selectedSociete; 
    public List<int> SelectedSociete 
    { 
     get 
     { 
      if (selectedSociete == null) 
      { 
       selectedSociete = Client.Societes.Select(m => m.ID).ToList(); 
      } 
      return selectedSociete; 
     } 
     set { selectedSociete = value; } 
    } 
} 
} 

そして最後に、私のビューを::

私は私のコントローラで使用してい

namespace CRM.Models 
{ 
public class Client : Personne // Héritage de la classe Personne 
{ 
    public Client() 
    { 
     this.Societes= new HashSet<Societe>(); 
    } 

    [Column("StatutClientID")] 

    public int StatutClientID { get; set; } 

    public string Fonction { get; set; } // TODO:Type enum 

    [Display(Name = "Est Actif ?")] 
    public bool IsActif { get; set; } 

    [DataType(DataType.MultilineText)] 
    public string Commentaire { get; set; } 

    public string Fax { get; set; } 

    public virtual StatutClient StatutClient { get; set; } 

    public virtual ICollection<Societe> Societes { get; set; } 
    override public string ToString() 
    { 
     return this.Prenom+" "+this.Nom; 
    } 

    } 

    }  

私のクラスのviewmodels:

public ActionResult Create() 
    { 
     var cli = new ClientViewModel 
     { 
      Client = new Client() 

     }; 

     var allSocietesList = db.Societes.ToList(); 

cli.AllSocietes = allSocietesList.Select(o => new SelectListItem 
     { 
      Text = o.Nom, 
      Value = o.ID.ToString() 
     }); 

     ViewBag.StatutClientID = new SelectList(db.StatutClients, "ID", "Designation"); 
     return View(cli); 
    } 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(ClientViewModel client) //Here is where the value dosesn't pass, "client" value is null; 
    { 
     if (ModelState.IsValid) 
     { 

      var cliToAdd = new Client(); 
      var updatedSocietes = new HashSet<int>(client.SelectedSociete); 
      foreach (Societe societe in db.Societes) 
      { 
       if (updatedSocietes.Contains(societe.ID)) 
       { 
        cliToAdd.Societes.Add((societe)); 
       } 

      } 
      db.Clients.Add(cliToAdd); 
      db.Entry(cliToAdd).State = System.Data.Entity.EntityState.Added; 

      db.SaveChanges(); 


      return RedirectToAction("Index"); 
     } 
     ViewBag.StatutClientID = new SelectList(db.StatutClients, "ID", "Designation", client.Client.StatutClientID); 
      return View(client); 

} 

は今私のクライアントモデルが登場

@model CRM.ViewModels.ClientViewModel 

@{ 
ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 


@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 

<div class="form-horizontal"> 
    <h4>Client</h4> 
    <hr /> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.Nom, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Client.Nom, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Client.Nom, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.Prenom, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Client.Prenom, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Client.Prenom, "", new { @class = "text-danger" }) 
     </div> 
    </div> 


    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.Fonction, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Client.Fonction, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Client.Fonction, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.Commentaire, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Client.Commentaire, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Client.Commentaire, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.Telephone, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Client.Telephone, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Client.Telephone, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.Mobile, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Client.Mobile, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Client.Mobile, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.Fax, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Client.Fax, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Client.Fax, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.Mail, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Client.Mail, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Client.Mail, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.IsActif, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      <div class="checkbox"> 
       @Html.EditorFor(model => model.Client.IsActif) 
       @Html.ValidationMessageFor(model => model.Client.IsActif, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.StatutClientID, "StatutClient", htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownList("StatutClientID", null, htmlAttributes: new { @class = "form-control" }) 
      @Html.ValidationMessageFor(model => model.Client.StatutClientID, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.AllSocietes, "JobTag", new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.ListBoxFor(model => model.SelectedSociete,  Model.AllSocietes) 
     </div> 
    </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> 
@Html.ActionLink("Retour à la liste", "Index") 
</div> 

@section Scripts { 
@System.Web.Optimization.Scripts.Render("~/bundles/jqueryval") 
} 

(私の社会モデル(名前 "societe")、それが何であるか知りたいが、ここから問題が出てこない場合は、スキップすることができます)。私の問題は、私の英語や他の何かの理解することは容易ではない場合は、ちょうど:)

EDITを尋ねる https://www.codeproject.com/articles/702890/mvc-entity-framework-and-many-to-many-relation

:首尾よく編集し、表示するための

namespace CRM.Models 
{ 
public class Societe 
{ 
    public Societe() 
    { 
     this.Clients = new HashSet<Client>(); 
    } 
    public int ID { get; set; } 
    public string Nom { get; set; } 
    //[Required] //TODO: renseigner adresse dans les societes de l'initializer 
    [Display(Name = "Lieu")] 
    public int LieuID { get; set; } 
    public string Adresse { get; set; } 
    public int Km { get; set; } 
    public string Temps { get; set; } 
    public bool IsActif { get; set; } 
    public virtual Lieu Lieu { get; set; } 
    public virtual ICollection<Affaire> Affaires { get; set; } 
    public virtual ICollection<Client> Clients { get; set; } 

} 
} 

は、私はそのチュートリアルに従っ:私の編集機能は100%機能です:

public ActionResult Edit(int? id) 
    { 
     if (id == null) 
     { 
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
     } 
     var clientViewModel = new ClientViewModel 
     { 
      Client = db.Clients.Include(i => i.Societes).First(i => i.ID == id) 
     }; 

     if (clientViewModel.Client == null) 
      return HttpNotFound(); 
     var allSocietesList = db.Societes.ToList(); 

     clientViewModel.AllSocietes = allSocietesList.Select(o => new SelectListItem 
     { 
      Text = o.Nom, 
      Value = o.ID.ToString() 
     }); 


     ViewBag.StatutClientID = new SelectList(db.StatutClients, "ID", "Designation", clientViewModel.Client.StatutClientID); 
     return View(clientViewModel); 
    } 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Edit(ClientViewModel clientView) 
    { 

     if (clientView == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 



     if (ModelState.IsValid) 
     { 
      var clientToUpdate = db.Clients 
       .Include(i => i.Societes).First(i => i.ID == clientView.Client.ID); 

      if (TryUpdateModel(clientToUpdate, "Client", new string[] { "Fonction", "Commentaire", "Nom", "Prenom", "Telephone", "Mobile", "Fax", "Mail", "IsActif", "StatutClientID" })) 
      { 
       // var newJobTags = db.Societes.Where(
       // m => clientView.SelectedSociete.Contains(m.ID)).ToList(); 
       var updatedSocietes = new HashSet<int>(clientView.SelectedSociete); 
       foreach (Societe societe in db.Societes) 
       { 
        if (!updatedSocietes.Contains(societe.ID)) 
        { 
         clientToUpdate.Societes.Remove(societe); 
        } 
        else 
        { 
         clientToUpdate.Societes.Add((societe)); 
        } 
       } 

       db.Entry(clientToUpdate).State = System.Data.Entity.EntityState.Modified; 
       db.SaveChanges(); 
      } 

      return RedirectToAction("Index"); 
     } 
     ViewBag.StatutClientID = new SelectList(db.StatutClients, "ID", "Designation", clientView.Client.StatutClientID); 
     return View(clientView); 
    } 

そして編集ビューに関連付けられている:

@model CRM.ViewModels.ClientViewModel 

@{ 
ViewBag.Title = "Edit"; 
} 

<h2 class="well">Clients - Edit</h2> 

@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 

<div class="form-horizontal"> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
    @Html.HiddenFor(model => model.Client.ID) 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.Fonction, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Client.Fonction, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Client.Fonction, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.Commentaire, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Client.Commentaire, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Client.Commentaire, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.Nom, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Client.Nom, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Client.Nom, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.Prenom, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Client.Prenom, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Client.Prenom, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.Telephone, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Client.Telephone, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Client.Telephone, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.Mobile, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Client.Mobile, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Client.Mobile, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.Fax, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Client.Fax, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Client.Fax, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.Mail, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Client.Mail, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Client.Mail, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.IsActif, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      <div class="checkbox"> 
       @Html.EditorFor(model => model.Client.IsActif) 
       @Html.ValidationMessageFor(model => model.Client.IsActif, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
    </div> 


    <div class="form-group"> 
     @Html.LabelFor(model => model.Client.StatutClientID, "Client.StatutClientID", htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownListFor(m => m.Client.StatutClientID, 
        (SelectList)ViewBag.StatutClientID, 
        Model.Client.StatutClient.ID); 

      @*Html.DropDownList("Client.StatutClientID", null, htmlAttributes: new { @class = "form-control" })*@ 
      @Html.ValidationMessageFor(model => model.Client.StatutClientID, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.AllSocietes, "JobTag", new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.ListBoxFor(m => m.SelectedSociete, Model.AllSocietes) 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" value="Save" class="btn btn-default" /> 
     </div> 
    </div> 
</div> 
} 

<div> 
@Html.ActionLink("Retour à la liste", "Index") 
</div> 

@section Scripts { 
@System.Web.Optimization.Scripts.Render("~/bundles/jqueryval") 
} 
+1

DBコンテキストにニーズコードを追加しましたか?そのようなもの:protected override void OnModelCreating(DbModelBuilder modelBuilder) {...} https://msdn.microsoft.com/en-us/library/jj591620(v=vs.113).aspxを参照してください。「多対多 - 多くの関係 " –

+0

こんにちは、あなたの答えに感謝します。いいえ、私はそれをしませんでした、私のコードは、最初にデータベースであり、編集はfunctionnalです。私は編集を使用しているときだけ私は1つの顧客のために必要な数の社会を追加することができます... –

+0

Isin't entity is auto to auto接合テーブルを作成する??? –

答えて

0

私が見ることができる唯一の問題はあなたの意見であり、残りは私にとっては大丈夫です。

まず第一に、私はしばしば私のフォームが掲載される予定ですし、どこ

@using (Html.BeginForm("Action_name", "Controler_name", FormMethod.Post, new { role = "form" })) 
{ 
} 

その後、あなたはまた、ページ内のモデルのIDを置くべきかを指定します。これは今私がいつもです。それはより一貫しています。そしてその部分をスキップすると、あなたのIDは利用できなくなります。ページが消滅しないようにする唯一の方法です。

のようなもの:

@Html.HiddenFor(t => t.client.id) 

あなたはそれが表示されなくても、戻ってあなたのフォームに渡されることを希望するすべてのモデルインスタンスのためにこれらを入れてください。私はそれがあなたの問題を完全に解決するかどうかは分かりませんが、それは良いスタートです。

+0

完了!あなたの答えをありがとうが、残念ながら、問題は明らかにそうではありませんでした。私の第2の方法「作成」はまだ私の社会の価値を奪いません。 –

+0

あなたの質問は歓迎です。 –