简体   繁体   English

MVC 将脚手架字段更新为下拉列表

[英]MVC Update Scaffolding field to dropdown

Web Dev noob here. Web 开发新手在这里。 I am currently using VS2019 and have created a MVC App to provide CRUD functionality (SQL) to inhouse users.我目前正在使用 VS2019 并创建了一个 MVC 应用程序来为内部用户提供 CRUD 功能 (SQL)。 We have a CostCentre field which should be a dropdown and not a text input.我们有一个 CostCentre 字段,它应该是一个下拉列表而不是文本输入。

How do I change the code to display dropdown instead of text input?如何更改代码以显示下拉列表而不是文本输入?

This is what contained in my controller:这是我的 controller 中包含的内容:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Magic,EmployeeCode,UserName,Entity,AD_SAM,MSTelNum,CostCentre,SysUser_AD,Sys_User_K8")] EmpCosDim empCosDim)
{
    if (ModelState.IsValid)
    {
        db.Entry(empCosDim).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(empCosDim);
}

Am I looking at the right place?我看对地方了吗?

UPDATE Edit View cshtml:更新编辑视图 cshtml:

@model EmployeeBilling.EmpCosDim
@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>EmpCosDim</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Magic)

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

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

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

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

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

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

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

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

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

UPDATE Added the ForeignKey as per the link you sent, but still does not display the dropdown.更新根据您发送的链接添加外键,但仍不显示下拉列表。 I did the change here:我在这里做了改变:

EmpCosDim.cs

// EmpCosDim.cs :

namespace EmployeeBilling
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;

    public class EmpCosDim
    {
        public int Magic { get; set; }
        public string EmployeeCode { get; set; }
        public string UserName { get; set; }
        public string Entity { get; set; }
        public string AD_SAM { get; set; }
        public string MSTelNum { get; set; }

        [ForeignKey("ContainingCostCentre")]
        public string CostCentre { get; set; }
        public virtual EmpCosDim ContainingCostCentre { get; set; }

        public string SysUser_AD { get; set; }
        public string Sys_User_K8 { get; set; }
    }
}

UPDATE So I've tried updating the CostCentre to a List in my EmpCosDim.cs file:更新所以我尝试将 CostCentre 更新为我的 EmpCosDim.cs 文件中的列表:

public class EmpCosDim
{
    [Key]
    public int Magic { get; set; }
    public string EmployeeCode { get; set; }
    public string UserName { get; set; }
    public string Entity { get; set; }
    public string AD_SAM { get; set; }
    public string MSTelNum { get; set; }

    public List<SelectCostCentreItem> CostCentre { get; set; }

    public string SysUser_AD { get; set; }
    public string Sys_User_K8 { get; set; }
}

public class SelectCostCentreItem
{
    public string CostCentre { get; set; }
}

But I'm getting an error on my Controller EmpCosDimsController.cs:但是我的 Controller EmpCosDimsController.cs 出现错误:

public ActionResult Index()
{
    return View(db.EmpCosDims.ToList());
}

错误

This seems to be because I replaced:这似乎是因为我更换了:

public string CostCentre { get; set; }

with

public List<SelectCostCentreItem> CostCentre { get; set; }

How can I update my controller to see the List and display in a dropdown?如何更新我的 controller 以查看列表并显示在下拉列表中?

The drop down list of CostCentre in your edit screen will actually display the list of all CostCentre that you want to be selected.在您的编辑屏幕中的 CostCentre 下拉列表实际上将显示您要选择的所有 CostCentre 的列表。 This list is not readily available in the EmpCosDim model and the view.此列表在 EmpCosDim model 和视图中不容易获得。 You need to build a selectlist and pass to your view and make change in view to get that list and display as dropdown.您需要构建一个选择列表并传递给您的视图并在视图中进行更改以获取该列表并显示为下拉列表。 Follow these steps -按着这些次序 -

In the Edit action (for Get not the post action) populate the view bag like this在 Edit 操作中(对于 Get 而不是 post 操作)像这样填充视图包

ViewBag.CostCentre = new SelectList([db.Concetres], ["Name"], ["Name"], 
[empCosDim.CostCentre]);

Here you have to replace the bracketed items with your appropriate variable/method.在这里,您必须用适当的变量/方法替换括号中的项目。 For the select list constructor first one is the source/list of the all costcenters, second one the field name of the EmpCosDim that you want to save as the value when it get selected, third one the field of the EmpCosDim that you want to display in the dropdown, and last one is the value you want to be selected by default.对于 select 列表构造函数,第一个是所有成本中心的源/列表,第二个是要在选择时保存为值的 EmpCosDim 的字段名称,第三个是要显示的 EmpCosDim 的字段在下拉列表中,最后一个是您要默认选择的值。

In the Edit view replace following code在编辑视图中替换以下代码

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

with the next code block与下一个代码块

<div class="form-group">
        @Html.LabelFor(model => model.CostCentre, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("CostCentre", (IEnumerable<SelectListItem>)ViewBag.CostCentre, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.CostCentre, "", new { @class = "text-danger" })
        </div>
 </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM