【问题标题】:How to use a Dropdownlist to select element from Database MVC CRUD?如何使用下拉列表从数据库 MVC CRUD 中选择元素?
【发布时间】:2020-04-17 06:06:09
【问题描述】:

我目前正在使用 .net core MVC 开发一个新的 Web 应用程序,我目前需要一个下拉列表来选择一个元素。 (我想显示名称并保存它的ID) 视图 (Edit.cshtml)

    @model AdminLTE.Models.Edits1 
    @{
        ViewBag.Title = "Edit";
    }
    <h2>Editar</h2>

    @using (Html.BeginForm())
    {
@Html.AntiForgeryToken()
<div class="form-horizontal">
    <h4>Edits</h4>
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.Id2)
    <div class="form-group">
        @Html.LabelFor(model => model.Id1, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Id1)
            @Html.ValidationMessageFor(model => model.Id1)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Codigo, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Codigo)
            @Html.ValidationMessageFor(model => model.Codigo)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Nome, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Nome)
            @Html.ValidationMessageFor(model => model.Nome)
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Guardar" class="btn btn-primary" />
            <a class="btn btn-default" asp-controller="Edit" asp-action="Index">Voltar</a>
        </div>
    </div>
</div>
    } 

控制器(编辑部分)

public ActionResult Edit(int? id)
 {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Edits edits = _context.Edits.Find(id);
            ViewBag.Othermodel = GetOthermodel();
            if (edits == null)
            {
                return HttpNotFound();
            }
            return View(edits);
}
private ActionResult HttpNotFound()
 {
   throw new HttpException(404, "Page Not Found");
 }
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind("Id2,Id1,Codigo,Nome")] Edits edits)
 {
    if (ModelState.IsValid)
      {
         _context.Entry(edits).State = EntityState.Modified;
         _context.SaveChanges();
         return RedirectToAction("Index");
      }
            return View(edits);
 } 
 private List<Othermodel> GetOthermodel()
    {
            List<Othermodel> othermodel = _context.Othermodel.ToList();
            return othermodel;
    }
 private List<Edits> GetEdits()
    {
            List<Edits> edits = _context.Edits.ToList();
            return edits;
    }

如何获取编辑表单以从正确的控制器 (Edits) 而不是 (Edits1) 获取数据,以及如何获取 Id1 的下拉列表(显示名称而不是 id) (对变量名称的一些更改)

【问题讨论】:

  • 这能回答你的问题吗? DropDownList in MVC 4 with Razor
  • @Vince 我需要它从数据库中的表中获取数据,将来可以通过网络应用程序扩展,我不确定 enum 将如何使用它。

标签: c# asp.net-mvc .net-core


【解决方案1】:

所以我设法让它显示一个下拉列表,但现在我需要一种方法来更新编辑。


    IEnumerable<Edit> edits = ViewData["Edits"] as IEnumerable<Edit>;
    IEnumerable<Othermodel> models = ViewData["Othermodels"] as IEnumerable<Othermodel>;
} ``` 
```   <div class="form-group">
        @Html.LabelFor(model => model.Id2, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <select id="dropdowntipo">
                <option value="0">Editse</option>
 @{
                    foreach (Othermodel model in model)
                    {
                        @foreach (Edit edit in edits)
                        {

                            <option value="@model.Id2">@model.Name</option>
                            @if (edit.Id2 == model.Id2)
                            {


                            }
                         }
                    }
                 }
            </select>
        </div>
    </div> ```

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多