通过控制器操作填充下拉列表

我必须dropdown list从 a填充 a List<string>,其中键等于显示的值。我来提取这些值,我做了各种测试来连接View但没有成功。给出提取值的代码:


public async Task<IActionResult> PopulateDropDownList()

{

    try

    {

        var items = await DocumentDBRepository<CosmosDBTelemetry>.GetDeviceIds();

        List<string> deviceids = new List<string>();


        foreach(var item in items)

        {

            deviceids.Add(item.deviceId);

        }


        return View();

      }

      catch (Exception e)

      {

        throw (e);

     }

}

剩下的事有人能帮我吗?


Qyouu
浏览 117回答 2
2回答

慕妹3146593

在模型-视图-控制器 (MVC) 框架中,控制器的工作是将模型传递给视图。假设您的字符串列表items已正确填充,控制器需要将其传递给视图。首先,您需要区分操作和辅助方法。操作是控制器类中的公共方法。用户可以通过 URL 访问操作,因此在您的情况下,http://application_name/controller_name/PopulateDropDownList将是有效的 URL,尽管这对用户来说毫无意义。相反,您需要创建PopulateDropDownList一个辅助方法,如下所示:private async Task<IEnumerable<string>> GetDropdownOptionsList(){  List<string> items = ...;  return items;}然后,从 URL 操作调用帮助程序,例如public async Task<IActionResult> View(){  List<string> items = await GetDropdownListOptions();  return View(items);}您可能希望查看此文档以获取有关控制器操作的信息。其次,该View()方法构造视图并将其发送给用户,但默认情况下,它不传递任何数据。View(items)您可以通过调用而不是将字符串列表传递到视图View()。然后,您的视图将如下所示:@model IEnumerable<string><select>  @foreach (string item in Model)  {    <option value="@item">@item</option>  }</select>该@model IEnumerable<string>指令指定视图需要将字符串列表传递到视图中。迭代@foreach (string item in Model)列表中的每个字符串并option为每个字符串生成一个元素。如果需要将多个数据模型传递到视图中,可以使用ViewBag或ViewData对象。只需像这样添加ViewBag.Items = items;到您的方法中:PopulateDropDownListprivate async void PopulateDropDownList(){  List<string> items = ...;  ViewBag.Items = items;}那么你的视图将如下所示:<select>  @foreach (string item in (IEnumerable<string>)ViewBag.Items)  {    <option value="@item">@item</option>  }</select>

一只甜甜圈

你有两个选择。选项1从控制器检索数据并从剃刀页面访问它。MyController.cspublic async Task<IActionResult> PopulateDropDownList(){&nbsp; &nbsp; var items = await&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DocumentDBRepository<CosmosDBTelemetry>.GetDeviceIds();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<string> deviceids = new List<string>();&nbsp; &nbsp; foreach(var item in items)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp;deviceids.Add(item.deviceId);&nbsp; &nbsp; }&nbsp; &nbsp; ViewData["deviceids"] = deviceids;&nbsp; &nbsp; return View();}MyViewPage.cshtml<select>&nbsp; &nbsp; &nbsp; &nbsp; <option value="">Select an option</option>&nbsp; &nbsp; &nbsp; @foreach (string deviceid in ((List<string>)ViewData["deviceids"]))&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; <option value="@deviceid">@deviceid</option>&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </select>&nbsp;选项2直接从 razor 页面检索和访问数据。MyViewPage.cshtml@Code&nbsp; &nbsp; var items =&nbsp; DocumentDBRepository<CosmosDBTelemetry>.GetDeviceIds();&nbsp; &nbsp; List<string> deviceids = new List<string>();&nbsp; &nbsp; foreach(var item in items)&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp;deviceids.Add(item.deviceId);&nbsp; &nbsp; &nbsp;}End Code<select>&nbsp; &nbsp; <option value="">Select an option</option>&nbsp; @foreach (string deviceid in deviceids)&nbsp; {&nbsp; &nbsp; <option value="@deviceid">@deviceid</option>&nbsp; }</select>&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP