在ASP.net中使用多对多的关系

时间:2016-11-28 15:32:48

标签: c# asp.net-mvc entity-framework many-to-many

我正在尝试使用多对多关系,我成功编辑并显示了一个客户(在我的代码中命名为“client”),但我无法创建。 在我的例子中,一个客户(客户)可以成为一个或多个社团的一部分。 一步一步地使用Visual Studio,我注意到我的值不能传递给我的第一个“创建”功能到第二个。我成功地获得了我想要的字段,但无法发布它们。 我的ClientController:

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

}

现在来了我的客户端模型:

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

   }

  }    

我在我的控制器中使用的ViewModel类:

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

最后,我的观点:

@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”),如果你想知道它上面有什么,但问题不是来自这里,你可以跳过它。)

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

}
}

为了成功编辑和显示,我遵循了该教程: https://www.codeproject.com/articles/702890/mvc-entity-framework-and-many-to-many-relation

如果因为我的英语或其他原因我的问题不容易理解,那就问问:)

编辑:这是我的编辑功能,它是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 个答案:

答案 0 :(得分:0)

我能看到的唯一问题是你的观点,其余的对我来说似乎没问题。

首先,我经常指定我的表格将如何发布以及

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

然后,您还应该将模型的ID放在页面中。这是我现在总是做的事情。它更连贯。如果你跳过这一部分,你的身份证书将无法使用,该页面是防止它消失的唯一方法。

类似的东西:

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

将这些内容放在您希望传递回表单的所有模型实例中,而不显示它们。我不知道它会完全解决你的问题,但这是一个好的开始。