问题描述
Web Dev noob在这里。 我目前正在使用VS2019,并已创建MVC应用程序以向内部用户提供CRUD功能(sql)。 我们有一个CostCentre字段,应该是一个下拉列表,而不是文本输入。
这是我的控制器中包含的内容:
[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);
}
我在找对地方吗?
更新 编辑视图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")
}
更新 根据您发送的链接添加了ForeignKey,但仍不显示下拉列表。我在这里做了更改:
// 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; }
}
}
更新 因此,我尝试将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; }
}
但是我的控制器EmpCosDimsController.cs出现错误:
public ActionResult Index()
{
return View(db.EmpCosDims.ToList());
}
这似乎是因为我替换了:
public string CostCentre { get; set; }
使用
public List<SelectCostCentreItem> CostCentre { get; set; }
如何更新控制器以查看列表并在下拉列表中显示?
解决方法
您的编辑屏幕中CostCentre的下拉列表实际上将显示您要选择的所有CostCentre的列表。该列表在EmpCosDim模型和视图中不可用。您需要构建一个选择列表,然后传递到视图并在视图中进行更改,以获取该列表并显示为下拉列表。 请按照以下步骤-
在“编辑”操作(对于“不获取帖子”操作中)中,这样填充视图包
ViewBag.CostCentre = new SelectList([db.Concetres],["Name"],[empCosDim.CostCentre]);
在这里,您必须用适当的变量/方法替换括号中的项目。对于 选择列表构造函数的第一个是所有成本中心的源/列表, 第二个是要保存为值的EmpCosDim的字段名称 被选中,第三个要显示在EmpCosDim的字段中 下拉菜单,最后一个是您要默认选择的值。
在“编辑”视图中,替换以下代码
<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.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>