0

我是编程实体框架 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>

档案控制器.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();
            }
        }
...
..
4

3 回答 3

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");
于 2013-10-01T14:48:08.697 回答
1

asp:DropDownList 是 ASP.NET,而不是 ASP.NET MVC。对于 MVC,您有 Html.DropDownList,您可以在控制器中加载实体,而不是视图。

于 2013-10-01T14:24:48.460 回答
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>
于 2013-10-02T15:47:52.513 回答