C# 在ASP.net中使用多对多关系
我尝试使用多对多关系,我成功地编辑和显示了一个客户(代码中的“客户”名称),但我无法创建。 在我的示例中,一个客户(客户)可以是一个或多个社会的一部分。 通过逐步使用VisualStudio,我注意到我的值无法传递到第一个“创建”函数和第二个“创建”函数。我成功地获得了我想要的字段,但无法发布它们。 我的客户控制器:C# 在ASP.net中使用多对多关系,c#,asp.net-mvc,entity-framework,many-to-many,C#,Asp.net Mvc,Entity Framework,Many To Many,我尝试使用多对多关系,我成功地编辑和显示了一个客户(代码中的“客户”名称),但我无法创建。 在我的示例中,一个客户(客户)可以是一个或多个社会的一部分。 通过逐步使用VisualStudio,我注意到我的值无法传递到第一个“创建”函数和第二个“创建”函数。我成功地获得了我想要的字段,但无法发布它们。 我的客户控制器: public ActionResult Create() { var cli = new ClientViewModel {
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.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; }
}
}
}
public ActionResult Create()
{
var cli=新的ClientViewModel
{
客户机=新客户机()
};
var allsocietestlist=db.Societes.ToList();
cli.AllSocietes=allSocietesList.Select(o=>new SelectListItem
{
Text=o.Nom,
值=o.ID.ToString()
});
ViewBag.statitClientId=新的选择列表(db.statitClients,“ID”,“Designation”);
返回视图(cli);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ClientViewModel client)//这里是值dosesn不能传递的地方,“client”值为null;
{
if(ModelState.IsValid)
{
var cliToAdd=新客户端();
var updatedSocietes=新哈希集(client.SelectedSociete);
foreach(法国兴业银行)
{
if(更新的Societes.Contains(societe.ID))
{
cliToAdd.Societes.添加((societe));
}
}
db.Clients.Add(cliToAdd);
db.Entry(cliToAdd.State=System.Data.Entity.EntityState.Added;
db.SaveChanges();
返回操作(“索引”);
}
ViewBag.statitClientId=新的选择列表(db.statitClients,“ID”,“Designation”,client.client.statitClientId);
返回视图(客户端);
}
现在是我的客户模型:
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;
}
}
}
namespace CRM.Models
{
公共类客户:Personne//Héritage de la classe Personne
{
公共客户机()
{
this.Societes=newhashset();
}
[列(“StatitClientID”)]
public int StatutClientID{get;set;}
公共字符串函数{get;set;}//TODO:类型枚举
[显示(Name=“Est Actif?”)]
公共bool IsActif{get;set;}
[数据类型(DataType.multilitext)]
公共字符串注释{get;set;}
公共字符串传真{get;set;}
公共虚拟StateTClient StateTClient{get;set;}
公共虚拟ICollection Societes{get;set;}
重写公共字符串ToString()
{
返回此.Prenom+“”+this.Nom;
}
}
}
我在控制器中使用的类视图模型:
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.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.ViewModels
{
公共类ClientViewModel
{
公共客户端{get;set;}
公共IEnumerable AllSocietes{get;set;}
私人名单选择兴业银行;
公众名单选择兴业银行
{
得到
{
如果(selectedSociete==null)
{
selectedSociete=Client.Societes.Select(m=>m.ID.ToList();
}
返回所选兴业银行;
}
设置{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")
}
@model CRM.ViewModels.ClientViewModel
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
客户
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.Client.Nom,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Client.Nom,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.Client.Nom,“,new{@class=“text danger”})
@LabelFor(model=>model.Client.Prenom,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Client.Prenom,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.Client.Prenom,“,new{@class=“text danger”})
@LabelFor(model=>model.Client.Fonction,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Client.Fonction,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.Client.Fonction,“,new{@class=“text danger”})
@LabelFor(model=>model.Client.Commentaire,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Client.Commentaire,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.Client.Commentaire,”,新的{@class=“text danger”})
@LabelFor(model=>model.Client.Telephone,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Client.Telephone,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Client.Telephone,“,new{@class=“text danger”})
@LabelFor(model=>model.Client.Mobile,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Client.Mobile,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.Client.Mobile,“,new{@class=“text danger”})
@LabelFor(model=>model.Client.Fax,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Client.Fax,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Client.Fax,“,new{@class=“text danger”})
@LabelFor(model=>model.Client.Mail,htmlA
@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")
}
@using (Html.BeginForm("Action_name", "Controler_name", FormMethod.Post, new { role = "form" }))
{
}
@Html.HiddenFor(t => t.client.id)