我試圖使用多對多的關係,我成功地編輯並顯示了一個客戶(名稱「客戶端」在我的代碼上),但我無法創建。 在我的例子中,一個客戶(客戶)可以是一個或多個社團的一部分。 一步一步使用Visual Studio,我發現我的值無法傳遞給第一個「創建」函數。我成功地獲得了我想要的領域,但無法發佈它們。 我ClientController:在ASP.net中使用多對多的關係
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;
}
}
}
我班的ViewModels我使用在我的控制器:
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%functionnal:
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")
}
您是否將需要代碼添加到您的數據庫上下文中?如:protected override void OnModelCreating(DbModelBuilder modelBuilder) {...}請參見https://msdn.microsoft.com/en-us/library/jj591620(v=vs.113).aspx「配置Many-to - 許多關係「 –
嗨,謝謝你的回答。不,我沒有那樣做,我的代碼是'數據庫'第一,編輯是功能,我可以添加儘可能多的社會我想要一個客戶只有當我使用編輯... –
Isin't實體應該自動創建結點表? –