【问题标题】:Selected Item Not Appearing Selected in DropDownList for Edit Page所选项目未出现在编辑页面的下拉列表中选择
【发布时间】:2020-03-01 05:32:27
【问题描述】:

我正在为我的工作制作一个基本的 MVC 站点,但我遇到了下拉列表的问题。我的编辑视图中有几个下拉列表并为教育者类添加视图,但在编辑视图中它们没有显示已选择的值(在教育者类中存储为标题),而是显示默认文本“--请选择- ——”。我希望在编辑条目时将下拉列表设置为正确的项目。

我在调试时查看了局部变量,发现 Model.uniqueTitles(教育工作者职称的 SelectList)确实包含所有职称,甚至有一个正确的标题被选中。但是当它进入视图时,没有选择任何内容。我尝试将 DropDownListFor 切换到 DropDownList 并发现了一些有趣的结果。对于 DropDownList,如果标题字符串(第一个输入变量)拼写不正确,则下拉菜单会选择正确的标题,但它无法保存,因为标题不正确,因此无法保存。我不确定为什么会这样。

EditEducator 视图

  /* This is not the entire view but it is the only part that is performing differently than expected */

<div class="form-group">
    @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.Title, (SelectList)Model.uniqueTitles, "-- Please Select --", new {@class = "form-control" })
        @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
    </div>
</div>

教育类

public class Educator
{
    public int EducatorID { get; set; } /* Only used when fetching data from Server to edit */
    [Required(AllowEmptyStrings = false, ErrorMessage = "Educator must have a first name.")]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }
    [Required(AllowEmptyStrings = false, ErrorMessage = "Educator must have a last name.")]
    [Display(Name = "Last Name")]
    public string LastName { get; set;  }
    [DataType(DataType.EmailAddress)]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Educator must have an email address.")]
    public string Email { get; set; }
    [DataType(DataType.PhoneNumber)]
    public string Phone { get; set; }
    [Required(AllowEmptyStrings = false, ErrorMessage = "Educator must have a title.")]
    public string Title { get; set; }
    public int CurrentEmployee { get; set; }
    public string State { get; set; }

    public SelectList uniqueTitles { get; set; }

    public Educator() { }

教育者控制器

public ActionResult EditEducator(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
        }
        var ed = LoadEducator(id); /* Loading requested educator into class instance */
        List<string> stringUniqueTitles = LoadUniqueTitles(); /* Loading all unique titles for educators */ 
        var tmpTitles = stringUniqueTitles.Select(x => new SelectListItem { Text = x, Value = x }).ToList(); 
        /* Turning them into List<SelectListItem> */
        var newUniqueSelect = new SelectList(tmpTitles, "Value", "Text", ed[0].Title); 
        /* Turning that into a SelectList with selected value of the title from the fetched educator */

        return View(new Models.Educator
        {
            EducatorID = ed[0].EducatorID,
            FirstName = ed[0].FirstName,
            LastName = ed[0].LastName,
            Email = ed[0].Email,
            Phone = ed[0].Phone,
            Title = ed[0].Title,
            CurrentEmployee = ed[0].CurrentEmployee,
            State = ed[0].State,
            uniqueTitles = newUniqueSelect

        });
    }

''''

没有收到错误消息,我只是无法在下拉菜单中接收到选定的输出。

编辑:我发现如果我将 DropDownList(而不是 DropDownListFor)的主要字段更改为不同的变量“editTitle”并在我的 Educator 类中定义它,那么下拉选择的值就可以正常工作。但是,当我提交时,我收到以下错误: System.NullReferenceException: 'Object reference not set to an instance of an object.'

【问题讨论】:

    标签: asp.net model-view-controller razor html.dropdownlistfor


    【解决方案1】:

    我很确定我已经找到了代码的问题所在。要根据其值选择 SelectListItem,该值应为整数。当我将 Value 切换为唯一的 int 时,下拉菜单选择了正确的值。感觉有点傻,但至少现在可以了。

    【讨论】:

      猜你喜欢
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多