我是编程实体框架MS的初学者
我有两个实体“档案”和“客户” 对于客户实体我设法“模型化”& “控制器”并列出并创建和删除。 我想用“文件夹”做同样的事情,知道每个文件都属于“客户”关系,然后: (dosiers.dos_CL_N = clienst.CL_N) 我发现在页面文件创建中使用“DropDownList”很困难 提前谢谢你的帮助 这是我的asp代码 的 Create.aspx
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.DOS_N) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.DOS_N) %>
<%: Html.ValidationMessageFor(model => model.DOS_N) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.DOS_CL_N) %>
</div>
<div class="editor-field">
<asp:DropDownList ID="ClientDropDownList" runat="server"
DataSourceID="ClientDataSource" DataTextField="CL_NOM" DataValueField="CL_N"
AppendDataBoundItems="True">
</asp:DropDownList>
<asp:EntityDataSource ID="ClientDataSource" runat="server"
ConnectionString="name=ClientsManagerDBEntities"
DefaultContainerName="ClientsManagerDBEntities" EnableFlattening="False"
EntitySetName="Clients" EntityTypeFilter="Clients"
Select="it.[CL_N], it.[CL_NOM]">
</asp:EntityDataSource>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "DossiersList")%>
</div>
DossierController.cs
....
...
// GET: /DossiersManager/Dossier/Create
public ActionResult Create()
{
return View();
}
//
// POST: /DossiersManager/Contact/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "Id")] Dossiers DossierToCreate)
{
if (!ModelState.IsValid)
return View();
try
{
_entities.AddToDossiers(DossierToCreate);
_entities.SaveChanges();
return RedirectToAction("DossiersList");
}
catch
{
return View();
}
}
...
..
答案 0 :(得分:1)
asp:DropDownList是ASP.NET,而不是ASP.NET MVC。对于MVC,您有Html.DropDownList,您可以在控制器中加载实体,而不是视图。
答案 1 :(得分:1)
尝试使用Html.DropDownListFor
助手而不是asp控件,
查看:强>
<%Html.DropDownListFor(model => model.YourProperty, (SelectList)ViewBag.ListValues)%>
<强>位指示:强>
List<SelectListItem> DropDownList = new List<SelectListItem>();
DropDownList.Add(new SelectListItem() { Value = "1", Text = "text 1"});
DropDownList.Add(new SelectListItem() { Value = "2", Text = "text 2"});
ViewBag.ListValues= new SelectList(DropDownList, "Value", "Text");
答案 2 :(得分:0)
谢谢大家 我找到了解决方案 我在DossierController.cs中添加了这段代码
private DossiersDBEntities DossiersDb=new DossiersDBEntities();
private ClientsDBEntities ClientsDb = new ClientsDBEntities();
public ActionResult Create()
{
var ListClients = from c in ClientsDb.Clients
select new
{
c.CL_N,
c.CL_NOM
};
ViewData["ListClients"] = new SelectList(ListClients, "CL_N", "CL_NOM");
return View();
我改变了我的asp页面创建方式
<div class="editor-field">
<%: Html.DropDownList("DOS_CL_N", (SelectList)ViewData["ListClients"])%>
<%: Html.ValidationMessageFor(model => model.DOS_CL_N) %>
</div>