【问题标题】:Why I am getting "System.Web.Mvc.SelectListItem" in my DropDownList?为什么我的 DropDownList 中出现“System.Web.Mvc.SelectListItem”?
【发布时间】:2018-01-30 19:35:37
【问题描述】:

我相信我已经正确绑定了我的数据,但我似乎无法让每个 SelectListItem 的文本属性正确显示。

我的模特:

public class Licenses
    {
        public SelectList LicenseNames { get; set; }
        public string SelectedLicenseName { get; set; }
    }

控制器:

[HttpGet]
    public ActionResult License()
    {
        try
        {
            DataTable LicsTable = BW.SQLServer.Table("GetLicenses", ConfigurationManager.ConnectionStrings["ProfressionalActivitiesConnection"].ToString());
            ProfessionalActivities.Models.Licenses model = new ProfessionalActivities.Models.Licenses();
            model.LicenseNames = new SelectList(LicsTable.AsEnumerable().Select(row =>
            new SelectListItem
            {
                Value = row["Description"].ToString(),
                Text = "test"
            }));
            return PartialView("_AddLicense", model);
        }
        catch (Exception ex)
        {
            var t = ex;
            return PartialView("_AddLicense");
        }
    }

查看:

@Html.DropDownList("LicenseNames", new SelectList(Model.LicenseNames, "Value", "Text", Model.LicenseNames.SelectedValue), new { htmlAttributes = new { @class = "form-control focusMe" } })

【问题讨论】:

  • 除了Shyju的回答,注意DropDownList()的第二个参数是IEnumerable<SelectListItem>。您可以使用 new SelectListItem { Value = row["Description"].ToString(), Text = "test"}) 创建它,这就是您所需要的。使用new SelectList()(您忘记设置TextValue 属性)在控制器中创建第二个相同的,然后在视图中创建第三个相同的只是毫无意义的额外开销。

标签: c# asp.net asp.net-mvc html-select


【解决方案1】:

使用LicenseNames 类型为SelectList 的属性的Items 属性

@Html.DropDownList("SelectedLicenseName", new SelectList(Model.LicenseNames.Items,
                                       "Value", "Text", Model.LicenseNames.SelectedValue))

或者使用 DropDownListFor 辅助方法

@Html.DropDownListFor(d=>d.SelectedLicenseName, 
                                         Model.LicenseNames.Items as List<SelectListItem>)

因此,当您发布表单时,您可以检查 SelectedLicenseName 属性

[HttpPost]
public ActionResult Create(Licenses model)
{
  //check model.SelectedLicenseName
}  

【讨论】:

  • 好的,可以。现在,当我发布时,如何从 SelectList 中获取选定的值以分配给我的 SelectedLicenseName 属性?
  • 查看我的更新答案。您需要使用表单元素名称作为“SelectedLicenseName”并在表单发布时阅读。
  • 所以使用您的第二个选项 - 使用 DropDownListForm 的选项,当我最初尝试调用局部视图时出现此错误。附加信息:没有具有键“SelectedLicenseName”的“IEnumerable”类型的 ViewData 项。
  • 您确定您使用的是DropDownListFor,因为它将使用您的模型属性(LicenseNames)来呈现下拉项目。这段代码对我来说很好。
  • 我有这个:@Html.DropDownListFor(model => model.SelectedLicenseName, Model.LicenseNames.Items as List, new { htmlAttributes = new { @class= "form-control focusMe" } })
【解决方案2】:

我明确设置了 dataValueFielddataTextField 名称。

new SelectListItem
{
    Value = row["Description"].ToString(),
    Text = "test"
}), "Value", "Text");

那么就没有必要在你的意见中写Model.LicenseNames.Items as List&lt;SelectListItem&gt;(正如你接受的答案中所建议的那样)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    相关资源
    最近更新 更多