【发布时间】:2013-04-29 14:35:00
【问题描述】:
我正在使用 C# 和 SQL Server 2005 开发一个 ASP .Net MVC 3 应用程序。
我也在使用实体框架和代码优先方法。
我有一个模型“Poste”,其中包含一些属性。
Poste 的视图(创建、编辑、删除...)是在我创建控制器时自动创建的。
我在这个模型中有一个外键。
此外键的值显示在“DropDownList”中。
问题是这些值与我的外键的右表无关。
解释更多:
我有 3 张桌子:
- Poste (ID_Poste, Nom_Poste,...,#ID_Ligne)
- Ligne (ID_Ligne, #ID_UF)
- UF (ID_UF)
在我的表单中,DropDownList 通常用于显示 ID_Ligne(Poste 表中的外键),但事实上,显示的值是表 UF (准确的 ID_UF)。
所以,它是表 UF 的投影。
不知道是什么原因。
我很抱歉英语,,, 不够清楚,我会尝试解释更多。
这是 Poste 的模型:
namespace MvcApplication2.Models
{
public class Poste
{
[Required]
[Key]
[Display(Name = "ID Poste :")]
public string ID_Poste { get; set; }
[Required]
[Display(Name = "Nom Poste:")]
public string nom_Poste { get; set; }
[Required]
[Display(Name = "Application :")]
public string Application { get; set; }
[Required]
[Display(Name = "In Poste :")]
public string In_Po { get; set; }
[Required]
[Display(Name = "Out Poste :")]
public string Out_Po { get; set; }
[Required]
[Display(Name = "Etat :")]
public string Etat { get; set; }
[Required]
[ForeignKey("Ligne")]
[Display(Name = "ID Ligne :")]
public string ID_Ligne { get; set; }
[Required]
[Display(Name = "Mouvement :")]
public string Mouvement { get; set; }
public virtual Ligne Ligne { get; set; }
public IEnumerable<Ligne> Lignes { get; set; }
public virtual ICollection<Poste> Postes { get; set; }
}
}
}
这是控制器:
namespace MvcApplication2.Controllers
{
public class PosteController : Controller
{
private GammeContext db = new GammeContext();
//
// GET: /Poste/
public ViewResult Index()
{
var postes = db.Postes.Include(p => p.Ligne);
return View(postes.ToList());
}
//
// GET: /Poste/Details/5
public ViewResult Details(string id)
{
Poste poste = db.Postes.Find(id);
return View(poste);
}
//
// GET: /Poste/Create
public ActionResult Create()
{
ViewBag.ID_Ligne = new SelectList(db.Lignes, "ID_Ligne", "ID_UF");
return View();
}
//
// POST: /Poste/Create
[HttpPost]
public ActionResult Create(Poste poste)
{
if (ModelState.IsValid)
{
db.Postes.Add(poste);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ID_Ligne = new SelectList(db.Lignes, "ID_Ligne", "ID_UF", poste.ID_Ligne);
return View(poste);
}
//
// GET: /Poste/Edit/5
public ActionResult Edit(string id)
{
Poste poste = db.Postes.Find(id);
ViewBag.ID_Ligne = new SelectList(db.Lignes, "ID_Ligne", "ID_UF", poste.ID_Ligne);
return View(poste);
}
//
// POST: /Poste/Edit/5
[HttpPost]
public ActionResult Edit(Poste poste)
{
if (ModelState.IsValid)
{
db.Entry(poste).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ID_Ligne = new SelectList(db.Lignes, "ID_Ligne", "ID_UF", poste.ID_Ligne);
return View(poste);
}
//
// GET: /Poste/Delete/5
public ActionResult Delete(string id)
{
Poste poste = db.Postes.Find(id);
return View(poste);
}
//
// POST: /Poste/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(string id)
{
Poste poste = db.Postes.Find(id);
db.Postes.Remove(poste);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
最后是 Create 视图:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage<MvcApplication2.Models.Poste>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Ajouter</h2>
<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>
<% using (Html.BeginForm()) { %>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Poste</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.ID_Poste) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.ID_Poste) %>
<%: Html.ValidationMessageFor(model => model.ID_Poste) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.nom_Poste) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.nom_Poste) %>
<%: Html.ValidationMessageFor(model => model.nom_Poste) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Application) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.Application) %>
<%: Html.ValidationMessageFor(model => model.Application) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.In_Po) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.In_Po) %>
<%: Html.ValidationMessageFor(model => model.In_Po) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Out_Po) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.Out_Po) %>
<%: Html.ValidationMessageFor(model => model.Out_Po) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Etat) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.Etat) %>
<%: Html.ValidationMessageFor(model => model.Etat) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.ID_Ligne, "Ligne") %>
</div>
<div class="editor-field">
<%: Html.DropDownList("ID_Ligne", String.Empty) %>
<%: Html.ValidationMessageFor(model => model.ID_Ligne) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Mouvement) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.Mouvement) %>
<%: Html.ValidationMessageFor(model => model.Mouvement) %>
</div>
<p>
<input type="submit" value="Ajouter" />
<input type="reset" value="Vider" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Retour à la liste", "Index") %>
</div>
</asp:Content>
Ps : 执行后会创建一个新的列(Poste_ID_Poste) 自动在我的基地中的 2 个表中:Poste 和 Ligne。
【问题讨论】:
标签: c# asp.net-mvc-3 sql-server-2005