[英]populate @html.listboxfor using stored procedure
I use entity with db first in mvc4(razor) 我首先在mvc4(razor)中使用带有db的实体
Requirement :I have sp returning id and display name, I need to display the names in @html.listboxfor i used interface to get the details from stored procedure.When I get the result in the controller from stored procedure,I got struck in saving the result to listbox, I couldnt figure out how to store it in listbox. 要求 :我有sp返回id和显示名称,我需要在@html.listbox中显示名称,我使用接口从存储过程中获取详细信息。当我从存储过程中获得控制器中的结果时,我得到了保存结果到列表框,我无法弄清楚如何将它存储在列表框中。 I got struck in the controller code.Please help.
我对控制器代码感到震惊。请帮助。
Code: 码:
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();
}
}
controller: 控制器:
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);
}
view: 视图:
@model project.ViewModels.ListboxViewModel
@Html.ListBoxFor( m=> m.resourcename,Model.resourcename, new { @class = "resList",style="height: 462px;"})
model: 模型:
public class ListboxViewModel
{
public string resourceid{get; set; }
//listbox Values
public List<SelectListItem> resourcename{get; set;}
}
Edit:GetResourceOrderDisplay_Result 编辑: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; }
}
}
Edit2: this is the error I'm getting after the update: Edit2:这是我在更新后得到的错误:
**Edit 3:**
Basically what you need to do is transform the list data you have retrieved from the database into a List<SelectListItem>
so it can be displayed. 基本上,您需要做的是将从数据库中检索到的
List<SelectListItem>
数据转换为List<SelectListItem>
以便显示它。 The Linq
Select
method makes this pretty easy: Linq
Select
方法使这很简单:
objListboxViewModel.resourcename =
listboxdata.Select(x => new SelectListItem() { Text = x.DisplayName,
Value = x.ID.ToString() })
.ToList();
So what you set the Text
and Value
properties to will depend on what the definition of GetResourceOrderDisplay_Result
looks like and what properties it has. 因此,将
Text
和Value
属性设置为将取决于GetResourceOrderDisplay_Result
的定义以及它具有的属性。 In the Select
method the x
represents a single element of the listboxdata
list and is of type GetResourceOrderDisplay_Result
, so you can access it's properties like x.Property
. 在
Select
方法中, x
表示listboxdata
列表的单个元素,类型为GetResourceOrderDisplay_Result
,因此您可以访问它的属性,如x.Property
。 The Select
method constructed in this way will return a new list of SelectListItem
. 以这种方式构造的
Select
方法将返回一个新的SelectListItem
列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.