【问题标题】:populate @html.listboxfor using stored procedure使用存储过程填充@html.listboxfor
【发布时间】:2023-03-17 23:29:01
【问题描述】:

我在 mvc4(razor) 中首先使用带有 db 的实体

要求:我有 sp 返回 id 和显示名称, 我需要在@html.listboxfor 中显示名称 我使用接口从存储过程中获取详细信息。当我从存储过程中获取控制器中的结果时,我在将结果保存到列表框时感到震惊, 我不知道如何将它存储在列表框中。 我在控制器代码中被击中。请帮助。

代码:

public class projService :Iproj
{
    project dbContext;

    public projService()
    {
        dbContext = new projectEntities();
    }

    public List<GetResourceOrderDisplay_Result> Getresorderdisplay()
    {
        List<GetResourceOrderDisplay_Result> oGetresorderdisplay = new List<GetResourceOrderDisplay_Result>();
        oGetresorderdisplay = dbContext.GetResourceOrderDisplay().ToList();
        return oGetresorderdisplay.ToList();
    }
}

控制器:

public ActionResult testview()
{
    List<GetResourceOrderDisplay_Result> listboxdata = new List<GetResourceOrderDisplay_Result>();
    listboxdata = _Scheduler.Getresorderdisplay();
    ListboxViewModel objListboxViewModel = new ListboxViewModel();

    //struck with the follwing line.
    objListboxViewModel.resourcename=listboxdata ????

    return View(objListboxViewModel); 
}

查看:

@model project.ViewModels.ListboxViewModel
@Html.ListBoxFor( m=> m.resourcename,Model.resourcename, new { @class = "resList",style="height: 462px;"})

型号:

public class ListboxViewModel
{
    public string resourceid{get; set; }
    //listbox Values
    public List<SelectListItem> resourcename{get; set;}
}

编辑:GetResourceOrderDisplay_Result

using System;
using System.Collections.Generic;

namespace proj.Data
{
    public partial class GetResourceOrderDisplay_Result
    {
        public int ID { get; set; }
        public string DisplayName { get; set; }
        }
   }

编辑2: 这是我在更新后得到的错误:

**Edit 3:**

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 razor entity-framework-4 listboxitem


    【解决方案1】:

    基本上,您需要做的是将您从数据库中检索到的列表数据转换为List&lt;SelectListItem&gt;,以便显示。 Linq Select 方法使这很容易:

    objListboxViewModel.resourcename =
        listboxdata.Select(x => new SelectListItem() { Text = x.DisplayName,
                                                       Value = x.ID.ToString() })
                   .ToList();
    

    因此,您将TextValue 属性设置为什么取决于GetResourceOrderDisplay_Result 的定义是什么样的以及它具有什么属性。在Select 方法中,x 表示listboxdata 列表的单个元素,并且是GetResourceOrderDisplay_Result 类型,因此您可以访问它的属性,例如x.Property。这样构造的Select方法会返回一个新的SelectListItem列表。

    【讨论】:

    • 非常感谢。如何从 sp 中获取文本和值的值,另请检查我的更新。
    • @gs11111 赞这个listboxdata.Select(x =&gt; new SelectListItem() { Text = x.DisplayName, Value = x.ID });
    • 我在 listboxdata 旁边收到一个错误 :(。请检查我的更新。
    • @gs11111 应该是 objListboxViewModel.resourcename 而不是 objListboxViewModel 您要分配列表。
    • @gs11111 哦,要么将您的模型更改为public IEnumerable&lt;SelectListItem&gt; resourcename{get; set;},要么在Select 表达式之后添加.ToList(),所以listboxdata.Select(...).ToList();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多