【问题标题】:Displaying data in a SelectList in ASP.NET Core在 ASP.NET Core 的 SelectList 中显示数据
【发布时间】:2016-10-03 15:43:53
【问题描述】:

我尝试了几种不同的方法。我不知道为什么,但我的 SelectList/DropDown 是空的。它没有显示任何数据。我不确定我哪里出错了。

我有一个 ASP.NET Core 应用程序。实体框架核心。数据库第一。我正在使用存储库模式。

这是我的模型类

public partial class Commodity
{
    public Guid Oid { get; set; }
    public string Code { get; set; }
}

这是我的界面:

interface ICommodityRepository
{
    IEnumerable<Commodity> GetAll();
}

我的仓库:

public class CommodityRepository : ICommodityRepository
{
    private ltgwarehouseContext context;

    public CommodityRepository()
    { }

    public IEnumerable<Commodity> GetAll()
    {
        return context.Commodity.ToList();
    }
}

我的控制器:

public class CommoditiesController : Controller
{
    static readonly CommodityRepository commodities = new CommodityRepository();

    public CommoditiesController(CommodityRepository commodities)
    { }

    // GET: /<controller>/
    public IEnumerable<Commodity> CommoditiesList()
    {
        return commodities.GetAll();
    }
}

这是我的视图/HTML 标记:

@model Lansing.BasisMap.Domain.Models.Commodity

&lt;li&gt;&lt;select asp-for="@Model.Code" asp-controller="Commodities" asp-action="CommoditiesList"&gt;&lt;/select&gt; &lt;/li&gt;

【问题讨论】:

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


    【解决方案1】:

    (我对 ASP.NET Core 中的 Tag Helper 语法不太熟悉,但我会试一试,如果我错了请大家纠正我)

    • asp-for="" 属性不需要@ 前缀,因为它不是 Razor 代码,属性值已经由 ASP.NET 的解析器处理 - 只有在使用与HTML(例如双引号)。
    • asp-controllerasp-action 属性不适用于 &lt;select&gt;
    • 您没有为您的&lt;select&gt; 提供任何选项,请使用asp-items 属性并提供IEnumerable&lt;SelectListItem&gt;SelectList 实例。这可以通过您的ViewModel 或(我的偏好)通过ViewData(或ViewBag)传递。

    假设它是ViewData,那么:

    public ActionResult YourControllerAction() {
    
        // stuff
        this.ViewData["items"] = commodities
            .GetAll()
            .Select( c => new SelectListItem() { Text = c.Code, Value = c.Oid.ToString() } )
            .ToList();
    
        // stuff
        return this.View( viewModel );
    }
    

    并像这样在视图中使用它:

    <select asp-for="Model.Code" asp-items="@ViewData["items"]" />
    

    此 QA 帖子中有更多示例:Select Tag Helper in ASP.NET Core MVC

    【讨论】:

    • 你好,很好的答案,但对我来说,我必须写 ...它显示,但不发送到后端(但那是另一个故事)
    【解决方案2】:

    试试这个:

    在视图中:

    <select asp-for="Staff.CityID" asp-items="@Model.CityList"></select>
    

    在控制器中:

    public IActionResult Create()
            {
                StaffViewModel model = new StaffViewModel();
                model.CityList = new List<SelectListItem>
                {
                    new SelectListItem {Text = "İstanbul", Value = "1"},
                    new SelectListItem {Text = "Sivas", Value = "2"}
                };
    
                return View(model);
            }
    

    在模型中:

    public class StaffViewModel
        {
            public  Staff Staff { get; set; }
            public  List<SelectListItem> CityList { get; set; }
        }
    

    【讨论】:

      【解决方案3】:

      这可能是因为您没有将数据库注入您的CommodityRepository。您需要将参数添加到构造函数才能使 DI 工作。

      public CommodityRepository(ltgwarehouseContext ltgwc)
      {
          context = ltgwc;
      }
      

      然后,如果您想让它自动填充并注入您的控制器,您需要使用services.Addxxx 方法之一在 Startup.cs 中注册它。我建议您阅读DI documentation,因为它会比我更好地解释它。

      您的最终控制器应如下所示:

      public class CommoditiesController : Controller
      {
          //Declare the local variable
          ICommodityRepository _commodities;
          //Load the repository via DI
          public CommoditiesController(CommodityRepository commodities)
          {
              //Set the local variable to the injected one
              _commodities = commodities;
          }
      
          // GET: /<controller>/
          public IEnumerable<Commodity> CommoditiesList()
          {
              //Reference the local variable in your methods
              return _commodities.GetAll();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-02
        • 2022-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多