首页 > 解决方案 > 在 Asp.net MVC 5 中编辑加入表格时完成下拉列表

问题描述

我有三个名为 Person 和 City 和 Evidence 的表,其表有几个列,包括 FirstName 、 LastName 、cityid (cityID)、 EvidenceID 表的 id (EvidenceID)、 EvidenceID 字段和 cityID 是外键,并到 id 表 City maps,Evidence connected

现在我想在创建一个新人时有两个组合框(标签),以便他们可以将它们列出来代替城市的 ID 和 Evidence 的 id,并将它们列出在牛仔框中。

另外,在编辑的时候,我想知道,当我进入编辑表格时,你可以在牛仔身上看到城市的名称和人物的简介,我们也可以看到和编辑其他的鹅卵石。请指导我。

[HttpGet] public ActionResult EditPerson(int id) { var q = (from a in db.Person where a.ID.Equals(id) select a).SingleOrDefault();

        if (q != null)
        {
            return View(q);
        }
        else
        {
            return RedirectToAction("ShowPerson", "Home");
        }
    }


    [HttpPost]
    public ActionResult EditPerson(Person per)
    {
        var q = (from a in db.Person
                 where a.ID.Equals(per.ID)
                 select a).SingleOrDefault();

        q.FirstName = per.FirstName;
        q.LastName = per.LastName;
        q.UserName = per.UserName;
        q.EvidenceID = per.EvidenceID;
        q.Mobile = per.Mobile;
        q.Stutus = false;
        q.CodeMelli = per.CodeMelli;
        q.CityID = per.CityID;
        q.Address = per.Address;
        q.Access = per.Access;
        q.Email = per.Email;
        q.Image = per.Image;

        db.Person.Attach(q);
        db.Entry(q).State = System.Data.Entity.EntityState.Modified;
        db.SaveChanges();

        return RedirectToAction("ShowPerson", "Home");
    }


    [HttpGet]
    public ActionResult CreatePerson()
    {

        return View();
    }


    [HttpPost]
    public ActionResult CreatePerson(Person per)
    {
        Person p = new Person();

        p.FirstName = per.FirstName;
        p.LastName = per.LastName;
        p.UserName = per.UserName;
        p.EvidenceID = per.EvidenceID;
        p.Mobile = per.Mobile;
        p.Stutus = false;
        p.CodeMelli = per.CodeMelli;
        p.CityID = per.CityID;

        p.Address = per.Address;
        p.Access = per.Access;
        p.Email = per.Email;
        p.Image = per.Image;

        db.Person.Add(p);
        db.SaveChanges();

        return RedirectToAction("ShowPerson", "Home");
    }

视图中的代码编辑:

        <div class="form-group">

@Html.LabelFor(model => model.CityID, htmlAttributes: new { @class = "control-label col-md-2" }) @Html.EditorFor(model => model.CityID, new { htmlAttributes = new { @ class = "form-control" } }) @Html.ValidationMessageFor(model => model.CityID, "", new { @class = "text-danger" })

        <div class="form-group">

@Html.LabelFor(model => model.EvidenceID, htmlAttributes: new { @class = "control-label col-md-2" }) @Html.EditorFor(model => model.EvidenceID, new { htmlAttributes = new { @ class = "form-control" } }) @Html.ValidationMessageFor(model => model.EvidenceID, "", new { @class = "text-danger" })

标签: asp.net-mvc-5asp.net-mvc-5.2

解决方案


你的问题没有被理解,但你需要的是来自三个不同模型的下拉列表,正如我猜测的那样......

在您自己的域中尝试此操作:

var dataForDL = _db.ModelName.Select(a => new
                {
                   Text = a.PersonName +" "+ a.Evidence.Name +" "+ a.City.CityName,
                   Value = a.Id
                }).ToList();
ViewBag.DropdownListForModel = new SelectList(dataForDL, "Value", "Text");

在 view.cshtml

@Html.DropDownList("DropdownListForModel", null, "-- Please Select Something --", htmlAttributes: new { @class = "form-control" })

推荐阅读